KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > TTCCLayout


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 // Contributors: Christopher Williams
18
// Mathias Bogaert
19

20 package org.apache.log4j;
21
22 import org.apache.log4j.helpers.DateLayout;
23 import org.apache.log4j.spi.LoggingEvent;
24
25 /**
26  TTCC layout format consists of time, thread, category and nested
27  diagnostic context information, hence the name.
28
29  <p>Each of the four fields can be individually enabled or
30  disabled. The time format depends on the <code>DateFormat</code>
31  used.
32
33  <p>Here is an example TTCCLayout output with the
34  {@link org.apache.log4j.helpers.RelativeTimeDateFormat}.
35
36  <pre>
37 176 [main] INFO org.apache.log4j.examples.Sort - Populating an array of 2 elements in reverse order.
38 225 [main] INFO org.apache.log4j.examples.SortAlgo - Entered the sort method.
39 262 [main] DEBUG org.apache.log4j.examples.SortAlgo.OUTER i=1 - Outer loop.
40 276 [main] DEBUG org.apache.log4j.examples.SortAlgo.SWAP i=1 j=0 - Swapping intArray[0] = 1 and intArray[1] = 0
41 290 [main] DEBUG org.apache.log4j.examples.SortAlgo.OUTER i=0 - Outer loop.
42 304 [main] INFO org.apache.log4j.examples.SortAlgo.DUMP - Dump of interger array:
43 317 [main] INFO org.apache.log4j.examples.SortAlgo.DUMP - Element [0] = 0
44 331 [main] INFO org.apache.log4j.examples.SortAlgo.DUMP - Element [1] = 1
45 343 [main] INFO org.apache.log4j.examples.Sort - The next log statement should be an error message.
46 346 [main] ERROR org.apache.log4j.examples.SortAlgo.DUMP - Tried to dump an uninitialized array.
47         at org.apache.log4j.examples.SortAlgo.dump(SortAlgo.java:58)
48         at org.apache.log4j.examples.Sort.main(Sort.java:64)
49 467 [main] INFO org.apache.log4j.examples.Sort - Exiting main method.
50 </pre>
51
52   <p>The first field is the number of milliseconds elapsed since the
53   start of the program. The second field is the thread outputting the
54   log statement. The third field is the level, the fourth field is
55   the category to which the statement belongs.
56
57   <p>The fifth field (just before the '-') is the nested diagnostic
58   context. Note the nested diagnostic context may be empty as in the
59   first two statements. The text after the '-' is the message of the
60   statement.
61
62   <p><b>WARNING</b> Do not use the same TTCCLayout instance from
63   within different appenders. The TTCCLayout is not thread safe when
64   used in his way. However, it is perfectly safe to use a TTCCLayout
65   instance from just one appender.
66
67   <p>{@link PatternLayout} offers a much more flexible alternative.
68
69   @author Ceki G&uuml;lc&uuml;
70   @author <A HREF="mailto:heinz.richter@ecmwf.int">Heinz Richter</a>
71
72 */

73 public class TTCCLayout extends DateLayout {
74
75   // Internal representation of options
76
private boolean threadPrinting = true;
77   private boolean categoryPrefixing = true;
78   private boolean contextPrinting = true;
79
80
81   protected final StringBuffer JavaDoc buf = new StringBuffer JavaDoc(256);
82
83
84   /**
85      Instantiate a TTCCLayout object with {@link
86      org.apache.log4j.helpers.RelativeTimeDateFormat} as the date
87      formatter in the local time zone.
88
89      @since 0.7.5 */

90   public TTCCLayout() {
91     this.setDateFormat(RELATIVE_TIME_DATE_FORMAT, null);
92   }
93
94
95   /**
96      Instantiate a TTCCLayout object using the local time zone. The
97      DateFormat used will depend on the <code>dateFormatType</code>.
98
99      <p>This constructor just calls the {@link
100      DateLayout#setDateFormat} method.
101
102      */

103   public TTCCLayout(String JavaDoc dateFormatType) {
104     this.setDateFormat(dateFormatType);
105   }
106
107
108   /**
109      The <b>ThreadPrinting</b> option specifies whether the name of the
110      current thread is part of log output or not. This is true by default.
111    */

112   public
113   void setThreadPrinting(boolean threadPrinting) {
114     this.threadPrinting = threadPrinting;
115   }
116
117   /**
118      Returns value of the <b>ThreadPrinting</b> option.
119    */

120   public
121   boolean getThreadPrinting() {
122     return threadPrinting;
123   }
124
125   /**
126      The <b>CategoryPrefixing</b> option specifies whether {@link Category}
127      name is part of log output or not. This is true by default.
128    */

129   public
130   void setCategoryPrefixing(boolean categoryPrefixing) {
131     this.categoryPrefixing = categoryPrefixing;
132   }
133
134   /**
135      Returns value of the <b>CategoryPrefixing</b> option.
136    */

137   public
138   boolean getCategoryPrefixing() {
139     return categoryPrefixing;
140   }
141
142   /**
143      The <b>ContextPrinting</b> option specifies log output will include
144      the nested context information belonging to the current thread.
145      This is true by default.
146    */

147   public
148   void setContextPrinting(boolean contextPrinting) {
149     this.contextPrinting = contextPrinting;
150   }
151
152   /**
153      Returns value of the <b>ContextPrinting</b> option.
154    */

155   public
156   boolean getContextPrinting() {
157     return contextPrinting;
158   }
159
160   /**
161    In addition to the level of the statement and message, the
162    returned byte array includes time, thread, category and {@link NDC}
163    information.
164
165    <p>Time, thread, category and diagnostic context are printed
166    depending on options.
167
168     @param event The event to format
169
170   */

171   public
172   String JavaDoc format(LoggingEvent event) {
173
174     // Reset buf
175
buf.setLength(0);
176
177     dateFormat(buf, event);
178
179     if(this.threadPrinting) {
180       buf.append('[');
181       buf.append(event.getThreadName());
182       buf.append("] ");
183     }
184     buf.append(event.getLevel().toString());
185     buf.append(' ');
186
187     if(this.categoryPrefixing) {
188       buf.append(event.getLoggerName());
189       buf.append(' ');
190     }
191
192     if(this.contextPrinting) {
193        String JavaDoc ndc = event.getNDC();
194
195       if(ndc != null) {
196     buf.append(ndc);
197     buf.append(' ');
198       }
199     }
200     buf.append("- ");
201     buf.append(event.getRenderedMessage());
202     buf.append(LINE_SEP);
203     return buf.toString();
204   }
205
206  /**
207      The TTCCLayout does not handle the throwable contained within
208      {@link LoggingEvent LoggingEvents}. Thus, it returns
209      <code>true</code>.
210
211      @since version 0.8.4 */

212   public
213   boolean ignoresThrowable() {
214     return true;
215   }
216 }
217
Popular Tags