View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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 chapters.configuration;
15  
16  /**
17   * Demonstrates programmatic invocation of Joran.
18   * 
19   */
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import ch.qos.logback.classic.LoggerContext;
24  import ch.qos.logback.classic.joran.JoranConfigurator;
25  import ch.qos.logback.core.joran.spi.JoranException;
26  import ch.qos.logback.core.util.StatusPrinter;
27  
28  public class MyApp3 {
29      final static Logger logger = LoggerFactory.getLogger(MyApp3.class);
30  
31      public static void main(String[] args) {
32          // assume SLF4J is bound to logback in the current environment
33          LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
34  
35          try {
36              JoranConfigurator configurator = new JoranConfigurator();
37              configurator.setContext(context);
38              // Call context.reset() to clear any previous configuration, e.g. default
39              // configuration. For multi-step configuration, omit calling context.reset().
40              context.reset();
41              configurator.doConfigure(args[0]);
42          } catch (JoranException je) {
43              // StatusPrinter will handle this
44          }
45          StatusPrinter.printInCaseOfErrorsOrWarnings(context);
46  
47          logger.info("Entering application.");
48  
49          Foo foo = new Foo();
50          foo.doIt();
51          logger.info("Exiting application.");
52      }
53  }