View Javadoc

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