001package ch.qos.logback.classic.layout; 002 003import ch.qos.logback.classic.pattern.ThrowableProxyConverter; 004import ch.qos.logback.classic.spi.ILoggingEvent; 005import ch.qos.logback.classic.spi.IThrowableProxy; 006import ch.qos.logback.core.CoreConstants; 007import ch.qos.logback.core.LayoutBase; 008import ch.qos.logback.core.util.CachingDateFormatter; 009 010/** 011 * A layout with a fixed format. The output is equivalent to that produced by 012 * {@link ch.qos.logback.classic.PatternLayout PatternLayout} with the pattern: 013 * </p> 014 * 015 * <pre> 016 * %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 017 * </pre> 018 * 019 * <p> 020 * TTLLLayout has the advantage of faster load time whereas 021 * {@link ch.qos.logback.classic.PatternLayout PatternLayout} requires roughly 022 * 40 milliseconds to load its parser classes. Note that the second run of 023 * PatternLayout will be much much faster (approx. 10 micro-seconds). 024 * </p> 025 * 026 * <p> 027 * Fixed format layouts such as TTLLLayout should be considered as an 028 * alternative to PatternLayout only if the extra 40 milliseconds at application 029 * start-up is considered significant. 030 * </p> 031 * 032 * @author Ceki Gülcü 033 * @since 1.1.6 034 */ 035public class TTLLLayout extends LayoutBase<ILoggingEvent> { 036 037 CachingDateFormatter cachingDateFormatter = new CachingDateFormatter("HH:mm:ss.SSS"); 038 ThrowableProxyConverter tpc = new ThrowableProxyConverter(); 039 040 @Override 041 public void start() { 042 tpc.start(); 043 super.start(); 044 } 045 046 @Override 047 public String doLayout(ILoggingEvent event) { 048 if (!isStarted()) { 049 return CoreConstants.EMPTY_STRING; 050 } 051 StringBuilder sb = new StringBuilder(); 052 053 long timestamp = event.getTimeStamp(); 054 055 sb.append(cachingDateFormatter.format(timestamp)); 056 sb.append(" ["); 057 sb.append(event.getThreadName()); 058 sb.append("] "); 059 sb.append(event.getLevel().toString()); 060 sb.append(" "); 061 sb.append(event.getLoggerName()); 062 sb.append(" - "); 063 sb.append(event.getFormattedMessage()); 064 sb.append(CoreConstants.LINE_SEPARATOR); 065 IThrowableProxy tp = event.getThrowableProxy(); 066 if (tp != null) { 067 String stackTrace = tpc.convert(event); 068 sb.append(stackTrace); 069 } 070 return sb.toString(); 071 } 072 073}