View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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 chapters.migrationFromLog4j;
15  
16  import ch.qos.logback.classic.spi.ILoggingEvent;
17  import ch.qos.logback.core.AppenderBase;
18  import ch.qos.logback.core.Layout;
19  
20  public class TrivialLogbackAppender extends AppenderBase<ILoggingEvent> {
21  
22      Layout<ILoggingEvent> layout;
23  
24      @Override
25      public void start() {
26          if (this.layout == null) {
27              addError("No layout set for the appender named [" + name + "].");
28              return;
29          }
30          String header = layout.getFileHeader();
31          System.out.println(header);
32          super.start();
33      }
34  
35      @Override
36      protected void append(ILoggingEvent loggingEvent) {
37          // note that AppenderBase.doAppend will invoke this method only if
38          // this appender was successfully started.
39          String eventAsStr = this.layout.doLayout(loggingEvent);
40          System.out.println(eventAsStr);
41      }
42  
43  
44      public Layout<ILoggingEvent> getLayout() {
45          return layout;
46      }
47  
48      public void setLayout(Layout<ILoggingEvent> layout) {
49          this.layout = layout;
50      }
51  
52  }