View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2007, 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 org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  import org.slf4j.MDC;
15  
16  import ch.qos.logback.classic.LoggerContext;
17  import ch.qos.logback.classic.joran.JoranConfigurator;
18  import ch.qos.logback.core.joran.spi.JoranException;
19  import ch.qos.logback.core.util.StatusPrinter;
20  import chapter4.sub.sample.Bar;
21  
22  /**
23   * 
24   * This class can be used to check the result of a configuration file.
25   * <p>
26   * When all the logback-core, logback-classic, logback-examples and their dependencies have been
27   * added to the ClassPath, one can launch this class using the following
28   * command:
29   * <p>
30   * java chapter4.ConfigurationTester
31   * chapter4/conf/name_of_the_configuration_file.xml
32   * 
33   * @author S&eacute;bastien Pennec
34   */
35  public class ConfigurationTester {
36  
37    public static void main(String[] args) throws InterruptedException {
38      Logger logger = (Logger) LoggerFactory.getLogger(ConfigurationTester.class);
39      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
40  
41      try {
42        JoranConfigurator configurator = new JoranConfigurator();
43        configurator.setContext(lc);
44        lc.reset();
45        configurator.doConfigure(args[0]);
46      } catch (JoranException je) {
47        je.printStackTrace();
48      }
49      // After we've called Joran, let's print information about the 
50      // internal status of logback
51      StatusPrinter.print(lc.getStatusManager());
52      
53      logger.debug("**Hello {}", new Bar());
54      MDC.put("testKey", "testValueFromMDC");
55      MDC.put("testKey2", "value2");
56      for (int i = 0; i < 10; i++) {
57        logger.debug("logging statement " + i);
58        Thread.sleep(1000);
59      }
60      Bar bar = new Bar();
61      bar.createLoggingRequest();
62    }
63  }