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