1
2
3
4
5
6
7
8
9
10
11
12
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
38
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 }