JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / org / apache / log4j / TTCCLayout.java
1 /*
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  */
17
18 // Contributors: Christopher Williams
19 //               Mathias Bogaert
20
21 package org.apache.log4j;
22
23 import org.apache.log4j.helpers.DateLayout;
24 import org.apache.log4j.spi.LoggingEvent;
25
26 /**
27  TTCC layout format consists of time, thread, category and nested
28  diagnostic context information, hence the name.
29
30  <p>Each of the four fields can be individually enabled or
31  disabled. The time format depends on the <code>DateFormat</code>
32  used.
33
34  <p>Here is an example TTCCLayout output with the
35  {@link org.apache.log4j.helpers.RelativeTimeDateFormat}.
36
37  <pre>
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.
51 </pre>
52
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.
57
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
61   statement.
62
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.
67
68   <p>{@link PatternLayout} offers a much more flexible alternative.
69
70   @author Ceki G&uuml;lc&uuml;
71   @author <A HREF="mailto:heinz.richter@ecmwf.int">Heinz Richter</a>
72
73 */
74 public class TTCCLayout extends DateLayout {
75
76   // Internal representation of options
77   private boolean threadPrinting    = true;
78   private boolean categoryPrefixing = true;
79   private boolean contextPrinting   = true;
80
81
82   protected final StringBuffer buf = new StringBuffer(256);
83
84
85   /**
86      Instantiate a TTCCLayout object with {@link
87      org.apache.log4j.helpers.RelativeTimeDateFormat} as the date
88      formatter in the local time zone.
89
90      @since 0.7.5 */
91   public TTCCLayout() {
92     this.setDateFormat(RELATIVE_TIME_DATE_FORMAT, null);
93   }
94
95
96   /**
97      Instantiate a TTCCLayout object using the local time zone. The
98      DateFormat used will depend on the <code>dateFormatType</code>.
99
100      <p>This constructor just calls the {@link
101      DateLayout#setDateFormat} method.
102
103      */
104   public TTCCLayout(String dateFormatType) {
105     this.setDateFormat(dateFormatType);
106   }
107
108
109   /**
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.
112    */
113   public
114   void setThreadPrinting(boolean threadPrinting) {
115     this.threadPrinting = threadPrinting;
116   }
117
118   /**
119      Returns value of the <b>ThreadPrinting</b> option.
120    */
121   public
122   boolean getThreadPrinting() {
123     return threadPrinting;
124   }
125
126   /**
127      The <b>CategoryPrefixing</b> option specifies whether {@link Category}
128      name is part of log output or not. This is true by default.
129    */
130   public
131   void setCategoryPrefixing(boolean categoryPrefixing) {
132     this.categoryPrefixing = categoryPrefixing;
133   }
134
135   /**
136      Returns value of the <b>CategoryPrefixing</b> option.
137    */
138   public
139   boolean getCategoryPrefixing() {
140     return categoryPrefixing;
141   }
142
143   /**
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.
147    */
148   public
149   void setContextPrinting(boolean contextPrinting) {
150     this.contextPrinting = contextPrinting;
151   }
152
153   /**
154      Returns value of the <b>ContextPrinting</b> option.
155    */
156   public
157   boolean getContextPrinting() {
158     return contextPrinting;
159   }
160
161   /**
162    In addition to the level of the statement and message, the
163    returned byte array includes time, thread, category and {@link NDC}
164    information.
165
166    <p>Time, thread, category and diagnostic context are printed
167    depending on options.
168
169     @param event The event to format
170
171   */
172   public
173   String format(LoggingEvent event) {
174
175     // Reset buf
176     buf.setLength(0);
177
178     dateFormat(buf, event);
179
180     if(this.threadPrinting) {
181       buf.append('[');
182       buf.append(event.getThreadName());
183       buf.append("] ");
184     }
185     buf.append(event.getLevel().toString());
186     buf.append(' ');
187
188     if(this.categoryPrefixing) {
189       buf.append(event.getLoggerName());
190       buf.append(' ');
191     }
192
193     if(this.contextPrinting) {
194        String ndc = event.getNDC();
195
196       if(ndc != null) {
197         buf.append(ndc);
198         buf.append(' ');
199       }
200     }
201     buf.append("- ");
202     buf.append(event.getRenderedMessage());
203     buf.append(LINE_SEP);
204     return buf.toString();
205   }
206
207  /**
208      The TTCCLayout does not handle the throwable contained within
209      {@link LoggingEvent LoggingEvents}. Thus, it returns
210      <code>true</code>.
211
212      @since version 0.8.4 */
213   public
214   boolean ignoresThrowable() {
215     return true;
216   }
217 }