View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
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();//this is to cancel default-config.
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  }