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 chapter4;
15  
16  import java.io.FileOutputStream;
17  import java.io.OutputStream;
18  import java.io.OutputStreamWriter;
19  
20  import org.slf4j.LoggerFactory;
21  
22  import ch.qos.logback.classic.Logger;
23  import ch.qos.logback.classic.LoggerContext;
24  import ch.qos.logback.classic.spi.ILoggingEvent;
25  import ch.qos.logback.core.WriterAppender;
26  import ch.qos.logback.core.layout.EchoLayout;
27  import ch.qos.logback.core.util.StatusPrinter;
28  
29  public class ExitWoes2 {
30  
31    public static void main(String[] args) throws Exception {
32      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
33      lc.reset();//this is to cancel default-config.
34      WriterAppender<ILoggingEvent> writerAppender = new WriterAppender<ILoggingEvent>();
35      writerAppender.setContext(lc);
36      writerAppender.setLayout(new EchoLayout<ILoggingEvent>());
37  
38      OutputStream os = new FileOutputStream("exitWoes2.log");
39      writerAppender.setWriter(new OutputStreamWriter(os));
40      writerAppender.setImmediateFlush(false);
41      writerAppender.start();
42      Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
43      root.addAppender(writerAppender);
44  
45      Logger logger = lc.getLogger(ExitWoes2.class);
46  
47      logger.debug("Hello world.");
48      
49      lc.stop();
50      
51      StatusPrinter.print(lc);
52    }
53  }