View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package chapter5;
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      StringBuffer sbuf = new StringBuffer(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  }