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 chapter7;
15  
16  import java.net.URL;
17  
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  import org.slf4j.MDC;
21  
22  import ch.qos.logback.classic.LoggerContext;
23  import ch.qos.logback.classic.PatternLayout;
24  import ch.qos.logback.classic.joran.JoranConfigurator;
25  import ch.qos.logback.classic.spi.ILoggingEvent;
26  import ch.qos.logback.core.ConsoleAppender;
27  import ch.qos.logback.core.joran.spi.JoranException;
28  import ch.qos.logback.core.util.Loader;
29  import ch.qos.logback.core.util.StatusPrinter;
30  
31  public class SimpleMDC {
32    static public void main(String[] args) throws Exception {
33      // You can put values in the MDC at any time. Before anything else 
34      // we put the first name
35      MDC.put("first", "Dorothy");
36  
37      // configure via the configuration file "chapter7/simpleMDC.xml"
38      // which ships with the examples
39      configureViaXML_File();
40      
41      // For educational purposes, the same configuration can 
42      // be accomplished programmatically.
43      // 
44      // programmaticConfiguration();
45      
46      Logger logger = LoggerFactory.getLogger(SimpleMDC.class);
47      // We now put the last name
48      MDC.put("last", "Parker");
49  
50      // The most beautiful two words in the English language according
51      // to Dorothy Parker:
52      logger.info("Check enclosed.");
53      logger.debug("The most beautiful two words in English.");
54  
55      MDC.put("first", "Richard");
56      MDC.put("last", "Nixon");
57      logger.info("I am not a crook.");
58      logger.info("Attributed to the former US president. 17 Nov 1973.");
59    }
60  
61    static void programmaticConfiguration() {
62      // Configure logback
63      LoggerContext loggerContext = (LoggerContext) LoggerFactory
64          .getILoggerFactory();
65      loggerContext.reset();
66      PatternLayout layout = new PatternLayout();
67      layout.setContext(loggerContext);
68      layout.setPattern("%X{first} %X{last} - %m%n");
69      layout.start();
70      ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
71      appender.setContext(loggerContext);
72      appender.setLayout(layout);
73      appender.start();
74      // cast root logger to c.q.logback.classic.Logger so that we can attach
75      // an appender to it
76      ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory
77          .getLogger("root");
78      root.addAppender(appender);
79    }
80  
81    static void configureViaXML_File() {
82      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
83      try {
84        JoranConfigurator configurator = new JoranConfigurator();
85        configurator.setContext(lc);
86        lc.stop();
87        URL url = Loader.getResourceBySelfClassLoader("chapter7/simpleMDC.xml");
88        configurator.doConfigure(url);
89      } catch (JoranException je) {
90        StatusPrinter.print(lc);
91      }
92    }
93  
94  }