1
2
3
4
5
6
7
8
9
10
11
12
13
14 package chapters.layouts;
15
16 import ch.qos.logback.classic.spi.ILoggingEvent;
17 import ch.qos.logback.core.CoreConstants;
18 import ch.qos.logback.core.LayoutBase;
19
20 public class MySampleLayout2 extends LayoutBase<ILoggingEvent> {
21
22 String prefix = null;
23 boolean printThreadName = true;
24
25 public void setPrefix(String prefix) {
26 this.prefix = prefix;
27 }
28
29 public void setPrintThreadName(boolean printThreadName) {
30 this.printThreadName = printThreadName;
31 }
32
33 public String doLayout(ILoggingEvent event) {
34 StringBuilder sbuf = new StringBuilder(128);
35 if (prefix != null) {
36 sbuf.append(prefix + ": ");
37 }
38 sbuf.append(event.getTimeStamp() - event.getLoggerContextVO().getBirthTime());
39 sbuf.append(" ");
40 sbuf.append(event.getLevel());
41 if (printThreadName) {
42 sbuf.append(" [");
43 sbuf.append(event.getThreadName());
44 sbuf.append("] ");
45 } else {
46 sbuf.append(" ");
47 }
48 sbuf.append(event.getLoggerName());
49 sbuf.append(" - ");
50 sbuf.append(event.getFormattedMessage());
51 sbuf.append(CoreConstants.LINE_SEPARATOR);
52 return sbuf.toString();
53 }
54 }