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  
28  public class ExitWoes1 {
29  
30    public static void main(String[] args) throws Exception {
31      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
32      lc.reset(); // we want to override the default-config.
33      WriterAppender<ILoggingEvent> writerAppender = new WriterAppender<ILoggingEvent>();
34      writerAppender.setContext(lc);
35      writerAppender.setLayout(new EchoLayout<ILoggingEvent>());
36  
37      OutputStream os = new FileOutputStream("exitWoes1.log");
38      writerAppender.setWriter(new OutputStreamWriter(os));
39      writerAppender.setImmediateFlush(false);
40      writerAppender.start();
41      Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
42      root.addAppender(writerAppender);
43  
44      Logger logger = lc.getLogger(ExitWoes1.class);
45  
46      logger.debug("Hello world.");
47    }
48  }