View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, 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 chapter3;
11  
12  /**
13   * Demonstrates programmatic invocation of Joran.
14   * 
15   */
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  
19  import ch.qos.logback.classic.LoggerContext;
20  import ch.qos.logback.classic.joran.JoranConfigurator;
21  import ch.qos.logback.core.joran.spi.JoranException;
22  import ch.qos.logback.core.util.StatusPrinter;
23  
24  public class MyApp3 {
25    final static Logger logger = LoggerFactory.getLogger(MyApp3.class);
26  
27    public static void main(String[] args) {
28      // assume SLF4J is bound to logback in the current environment
29      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
30  
31      try {
32        JoranConfigurator configurator = new JoranConfigurator();
33        configurator.setContext(lc);
34        // the context was probably already configured by default configuration
35        // rules
36        lc.reset();
37        configurator.doConfigure(args[0]);
38      } catch (JoranException je) {
39        je.printStackTrace();
40      }
41      StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
42  
43      logger.info("Entering application.");
44  
45      Foo foo = new Foo();
46      foo.doIt();
47      logger.info("Exiting application.");
48    }
49  }