1
2
3
4
5
6
7
8
9
10 package chapter4;
11
12 import java.io.FileOutputStream;
13 import java.io.OutputStream;
14 import java.io.OutputStreamWriter;
15
16 import org.slf4j.LoggerFactory;
17
18 import ch.qos.logback.classic.Logger;
19 import ch.qos.logback.classic.LoggerContext;
20 import ch.qos.logback.classic.spi.LoggingEvent;
21 import ch.qos.logback.core.WriterAppender;
22 import ch.qos.logback.core.layout.EchoLayout;
23
24 public class ExitWoes1 {
25
26 public static void main(String[] args) throws Exception {
27 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
28 lc.shutdownAndReset();
29 WriterAppender<LoggingEvent> writerAppender = new WriterAppender<LoggingEvent>();
30 writerAppender.setContext(lc);
31 writerAppender.setLayout(new EchoLayout<LoggingEvent>());
32
33 OutputStream os = new FileOutputStream("exitWoes1.log");
34 writerAppender.setWriter(new OutputStreamWriter(os));
35 writerAppender.setImmediateFlush(false);
36 writerAppender.start();
37 Logger root = lc.getLogger(LoggerContext.ROOT_NAME);
38 root.addAppender(writerAppender);
39
40 Logger logger = lc.getLogger(ExitWoes1.class);
41
42 logger.debug("Hello world.");
43 }
44 }