View Javadoc
1   package ch.qos.logback.classic.layout;
2   
3   import ch.qos.logback.classic.pattern.ThrowableProxyConverter;
4   import ch.qos.logback.classic.spi.ILoggingEvent;
5   import ch.qos.logback.classic.spi.IThrowableProxy;
6   import ch.qos.logback.core.CoreConstants;
7   import ch.qos.logback.core.LayoutBase;
8   import ch.qos.logback.core.util.CachingDateFormatter;
9   
10  /**
11   * A layout with a fixed format. The output is equivalent to that produced by {@link ch.qos.logback.classic.PatternLayout PatternLayout} with the pattern:</p>
12   * 
13   * <pre>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pre>
14   * 
15   *<p>TTLLLayout has the advantage of faster load time whereas {@link ch.qos.logback.classic.PatternLayout PatternLayout}
16   * requires roughly 40 milliseconds to load its parser classes.  Note that the second run of PatternLayout will be much much faster (approx. 10 micro-seconds).</p>
17   * 
18   * <p>Fixed format layouts such as TTLLLayout should be considered as an alternative to PatternLayout only if the extra 40 milliseconds at application start-up is considered significant.</p>
19   * 
20   * @author Ceki G&uuml;lc&uuml;
21   * @since 1.1.6
22   */
23  public class TTLLLayout extends LayoutBase<ILoggingEvent> {
24  
25      CachingDateFormatter cachingDateFormatter = new CachingDateFormatter("HH:mm:ss.SSS");
26      ThrowableProxyConverter tpc = new ThrowableProxyConverter();
27  
28      @Override
29      public void start() {
30          tpc.start();
31          super.start();
32      }
33  
34      @Override
35      public String doLayout(ILoggingEvent event) {
36          if (!isStarted()) {
37              return CoreConstants.EMPTY_STRING;
38          }
39          StringBuilder sb = new StringBuilder();
40  
41          long timestamp = event.getTimeStamp();
42  
43          sb.append(cachingDateFormatter.format(timestamp));
44          sb.append(" [");
45          sb.append(event.getThreadName());
46          sb.append("] ");
47          sb.append(event.getLevel().toString());
48          sb.append(" ");
49          sb.append(event.getLoggerName());
50          sb.append(" - ");
51          sb.append(event.getFormattedMessage());
52          sb.append(CoreConstants.LINE_SEPARATOR);
53          IThrowableProxy tp = event.getThrowableProxy();
54          if (tp != null) {
55              String stackTrace = tpc.convert(event);
56              sb.append(stackTrace);
57          }
58          return sb.toString();
59      }
60  
61  }