2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 // Contributors: Christopher Williams
21 package org.apache.log4j;
23 import org.apache.log4j.helpers.DateLayout;
24 import org.apache.log4j.spi.LoggingEvent;
27 TTCC layout format consists of time, thread, category and nested
28 diagnostic context information, hence the name.
30 <p>Each of the four fields can be individually enabled or
31 disabled. The time format depends on the <code>DateFormat</code>
34 <p>Here is an example TTCCLayout output with the
35 {@link org.apache.log4j.helpers.RelativeTimeDateFormat}.
38 176 [main] INFO org.apache.log4j.examples.Sort - Populating an array of 2 elements in reverse order.
39 225 [main] INFO org.apache.log4j.examples.SortAlgo - Entered the sort method.
40 262 [main] DEBUG org.apache.log4j.examples.SortAlgo.OUTER i=1 - Outer loop.
41 276 [main] DEBUG org.apache.log4j.examples.SortAlgo.SWAP i=1 j=0 - Swapping intArray[0] = 1 and intArray[1] = 0
42 290 [main] DEBUG org.apache.log4j.examples.SortAlgo.OUTER i=0 - Outer loop.
43 304 [main] INFO org.apache.log4j.examples.SortAlgo.DUMP - Dump of interger array:
44 317 [main] INFO org.apache.log4j.examples.SortAlgo.DUMP - Element [0] = 0
45 331 [main] INFO org.apache.log4j.examples.SortAlgo.DUMP - Element [1] = 1
46 343 [main] INFO org.apache.log4j.examples.Sort - The next log statement should be an error message.
47 346 [main] ERROR org.apache.log4j.examples.SortAlgo.DUMP - Tried to dump an uninitialized array.
48 at org.apache.log4j.examples.SortAlgo.dump(SortAlgo.java:58)
49 at org.apache.log4j.examples.Sort.main(Sort.java:64)
50 467 [main] INFO org.apache.log4j.examples.Sort - Exiting main method.
53 <p>The first field is the number of milliseconds elapsed since the
54 start of the program. The second field is the thread outputting the
55 log statement. The third field is the level, the fourth field is
56 the category to which the statement belongs.
58 <p>The fifth field (just before the '-') is the nested diagnostic
59 context. Note the nested diagnostic context may be empty as in the
60 first two statements. The text after the '-' is the message of the
63 <p><b>WARNING</b> Do not use the same TTCCLayout instance from
64 within different appenders. The TTCCLayout is not thread safe when
65 used in his way. However, it is perfectly safe to use a TTCCLayout
66 instance from just one appender.
68 <p>{@link PatternLayout} offers a much more flexible alternative.
70 @author Ceki Gülcü
71 @author <A HREF="mailto:heinz.richter@ecmwf.int">Heinz Richter</a>
74 public class TTCCLayout extends DateLayout {
76 // Internal representation of options
77 private boolean threadPrinting = true;
78 private boolean categoryPrefixing = true;
79 private boolean contextPrinting = true;
82 protected final StringBuffer buf = new StringBuffer(256);
86 Instantiate a TTCCLayout object with {@link
87 org.apache.log4j.helpers.RelativeTimeDateFormat} as the date
88 formatter in the local time zone.
92 this.setDateFormat(RELATIVE_TIME_DATE_FORMAT, null);
97 Instantiate a TTCCLayout object using the local time zone. The
98 DateFormat used will depend on the <code>dateFormatType</code>.
100 <p>This constructor just calls the {@link
101 DateLayout#setDateFormat} method.
104 public TTCCLayout(String dateFormatType) {
105 this.setDateFormat(dateFormatType);
110 The <b>ThreadPrinting</b> option specifies whether the name of the
111 current thread is part of log output or not. This is true by default.
114 void setThreadPrinting(boolean threadPrinting) {
115 this.threadPrinting = threadPrinting;
119 Returns value of the <b>ThreadPrinting</b> option.
122 boolean getThreadPrinting() {
123 return threadPrinting;
127 The <b>CategoryPrefixing</b> option specifies whether {@link Category}
128 name is part of log output or not. This is true by default.
131 void setCategoryPrefixing(boolean categoryPrefixing) {
132 this.categoryPrefixing = categoryPrefixing;
136 Returns value of the <b>CategoryPrefixing</b> option.
139 boolean getCategoryPrefixing() {
140 return categoryPrefixing;
144 The <b>ContextPrinting</b> option specifies log output will include
145 the nested context information belonging to the current thread.
146 This is true by default.
149 void setContextPrinting(boolean contextPrinting) {
150 this.contextPrinting = contextPrinting;
154 Returns value of the <b>ContextPrinting</b> option.
157 boolean getContextPrinting() {
158 return contextPrinting;
162 In addition to the level of the statement and message, the
163 returned byte array includes time, thread, category and {@link NDC}
166 <p>Time, thread, category and diagnostic context are printed
167 depending on options.
169 @param event The event to format
173 String format(LoggingEvent event) {
178 dateFormat(buf, event);
180 if(this.threadPrinting) {
182 buf.append(event.getThreadName());
185 buf.append(event.getLevel().toString());
188 if(this.categoryPrefixing) {
189 buf.append(event.getLoggerName());
193 if(this.contextPrinting) {
194 String ndc = event.getNDC();
202 buf.append(event.getRenderedMessage());
203 buf.append(LINE_SEP);
204 return buf.toString();
208 The TTCCLayout does not handle the throwable contained within
209 {@link LoggingEvent LoggingEvents}. Thus, it returns
212 @since version 0.8.4 */
214 boolean ignoresThrowable() {