[logback-dev] svn commit: r1671 - in logback/trunk: logback-examples/src/main/java/chapter7 logback-site/src/site/pages/manual

noreply.ceki at qos.ch noreply.ceki at qos.ch
Sat Apr 12 01:18:33 CEST 2008


Author: ceki
Date: Sat Apr 12 01:18:33 2008
New Revision: 1671

Modified:
   logback/trunk/logback-examples/src/main/java/chapter7/SimpleMDC.java
   logback/trunk/logback-site/src/site/pages/manual/mdc.html

Log:
- correcting documentation and SimpleMDC code, in light of problems reported [1] 
  on the logback mailing list by Daniel King

[1] http://www.qos.ch/pipermail/logback-user/2008-April/000427.html

Modified: logback/trunk/logback-examples/src/main/java/chapter7/SimpleMDC.java
==============================================================================
--- logback/trunk/logback-examples/src/main/java/chapter7/SimpleMDC.java	(original)
+++ logback/trunk/logback-examples/src/main/java/chapter7/SimpleMDC.java	Sat Apr 12 01:18:33 2008
@@ -10,36 +10,37 @@
 
 package chapter7;
 
+import java.net.URL;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.MDC;
 
+import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.PatternLayout;
+import ch.qos.logback.classic.joran.JoranConfigurator;
 import ch.qos.logback.classic.spi.LoggingEvent;
 import ch.qos.logback.core.ConsoleAppender;
+import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.util.Loader;
+import ch.qos.logback.core.util.StatusPrinter;
 
 public class SimpleMDC {
   static public void main(String[] args) throws Exception {
-    // You can put values in the MDC at any time. We first put the
-    // first name
+    // You can put values in the MDC at any time. Before anything else 
+    // we put the first name
     MDC.put("first", "Dorothy");
 
-    // Configure logback
-    PatternLayout layout = new PatternLayout();
-    layout.setPattern("%X{first} %X{last} - %m%n");
-    layout.start();
-    ConsoleAppender<LoggingEvent> appender = new ConsoleAppender<LoggingEvent>();
-    appender.setLayout(layout);
-    appender.start();
-    // cast root logger to c.q.logback.classic.Logger so that we can attach an
-    // appender to it
-    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory
-        .getLogger("root");
-    root.addAppender(appender);
-
-    // get another logger
+    // configure via the configuration file "chapter7/simpleMDC.xml"
+    // which ships with the examples
+    configureViaXML_File();
+    
+    // For educational purposes, the same configuration can 
+    // be accomplished programmatically.
+    // 
+    // programmaticConfiguration();
+    
     Logger logger = LoggerFactory.getLogger(SimpleMDC.class);
-
     // We now put the last name
     MDC.put("last", "Parker");
 
@@ -53,4 +54,38 @@
     logger.info("I am not a crook.");
     logger.info("Attributed to the former US president. 17 Nov 1973.");
   }
+
+  static void programmaticConfiguration() {
+    // Configure logback
+    LoggerContext loggerContext = (LoggerContext) LoggerFactory
+        .getILoggerFactory();
+    loggerContext.shutdownAndReset();
+    PatternLayout layout = new PatternLayout();
+    layout.setContext(loggerContext);
+    layout.setPattern("%X{first} %X{last} - %m%n");
+    layout.start();
+    ConsoleAppender<LoggingEvent> appender = new ConsoleAppender<LoggingEvent>();
+    appender.setContext(loggerContext);
+    appender.setLayout(layout);
+    appender.start();
+    // cast root logger to c.q.logback.classic.Logger so that we can attach
+    // an appender to it
+    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory
+        .getLogger("root");
+    root.addAppender(appender);
+  }
+
+  static void configureViaXML_File() {
+    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+    try {
+      JoranConfigurator configurator = new JoranConfigurator();
+      configurator.setContext(lc);
+      lc.shutdownAndReset();
+      URL url = Loader.getResourceByTCL("chapter7/simpleMDC.xml");
+      configurator.doConfigure(url);
+    } catch (JoranException je) {
+      StatusPrinter.print(lc);
+    }
+  }
+
 }

Modified: logback/trunk/logback-site/src/site/pages/manual/mdc.html
==============================================================================
--- logback/trunk/logback-site/src/site/pages/manual/mdc.html	(original)
+++ logback/trunk/logback-site/src/site/pages/manual/mdc.html	Sat Apr 12 01:18:33 2008
@@ -112,29 +112,16 @@
 
 public class SimpleMDC {
   static public void main(String[] args) throws Exception {
-    // You can put values in the MDC at any time. We first put the
-    // first name
-    <b>MDC.put("first", "Dorothy");</b>
-
-    // Configure logback
-    PatternLayout layout = new PatternLayout();
-    layout.setPattern("%X{first} %X{last} - %m%n");
-    layout.start();
-    ConsoleAppender&lt;LoggingEvent> appender = new ConsoleAppender&lt;LoggingEvent>();
-    appender.setLayout(layout);
-    appender.start();
-    
-    // cast root logger to c.q.logback.classic.Logger so that we can attach an
-    // appender to it
-    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory
-        .getLogger("root");
-    root.addAppender(appender);    
 
-    // get another logger
-    Logger logger = (Logger)LoggerFactory.getLogger(SimpleMDC.class);
+   // You can put values in the MDC at any time. Before anything else 
+    // we put the first name
+    MDC.put("first", "Dorothy");
 
+    [ SNIP ]
+    
+    Logger logger = LoggerFactory.getLogger(SimpleMDC.class);
     // We now put the last name
-    <b>MDC.put("last", "Parker");</b>
+    MDC.put("last", "Parker");
 
     // The most beautiful two words in the English language according
     // to Dorothy Parker:
@@ -146,22 +133,42 @@
     logger.info("I am not a crook.");
     logger.info("Attributed to the former US president. 17 Nov 1973.");
   }
+
+  [ SNIP ]
+
 }</pre></div>
 
-		<p>
-			The main method starts by associating the value <em>Dorothy</em> with 
-			the key <em>first</em> in the <code>MDC</code>. You can place as many 
-			value/key associations in the <code>MDC</code> as you wish. 
-			Multiple insertions with the same key will overwrite older values. 
-			The code then proceeds to configure logback. 
-			Note the usage of the <em>%X</em> specifier within the 
-			<code>PatternLayout</code> conversion pattern. The <em>%X</em> 
-			conversion specifier is employed twice, once for the key <em>first</em> 
-			and once for the key <em>last</em>. After configuring the root logger, 
-			the code associates the value <em>Parker</em> with the key <em>last</em>. 
-			It then invokes the logger twice with different messages. 
-			The code finishes by setting the <code>MDC</code> to different values 
-			and issuing several logging requests. Running SimpleMDC yields:
+		<p>The main method starts by associating the value
+		<em>Dorothy</em> with the key <em>first</em> in the
+		<code>MDC</code>. You can place as many value/key associations in
+		the <code>MDC</code> as you wish.  Multiple insertions with the
+		same key will overwrite older values.  The code then proceeds to
+		configure logback.</p>
+
+    <p>For the sake of consiceness, we have the omitted the code that
+    configures logback with the configuration file <a
+    href="http://tinyurl.com/4gy542">simpleMDC.xml</a>. Here is the
+    relevant section from that file.
+    </p>
+
+ <p class="source">&lt;appender name="CONSOLE"
+    class="ch.qos.logback.core.ConsoleAppender"> &lt;layout
+    class="ch.qos.logback.classic.PatternLayout">
+    &lt;Pattern><b>%X{first} %X{last}</b> - %m%n&lt;/Pattern>
+    &lt;/layout> &lt;/appender></p>
+    
+
+
+    <p>Note the usage of the <em>%X</em> specifier within the
+    <code>PatternLayout</code> conversion pattern. The <em>%X</em>
+    conversion specifier is employed twice, once for the key named
+    <em>first</em> and once for the key named <em>last</em>. After
+    obtaining a logger corresponding to <code>SimpleMDC.class</code>,
+    the code associates the value <em>Parker</em> with the key named
+    <em>last</em>.  It then invokes the logger twice with different
+    messages.  The code finishes by setting the <code>MDC</code> to
+    different values and issuing several logging requests. Running
+    SimpleMDC yields:
 		</p>
 
 <div class="source"><pre>Dorothy Parker - Check enclosed.
@@ -170,11 +177,11 @@
 Richard Nixon - Attributed to the former US president. 17 Nov 1973.</pre></div>
 
 
-		<p>
-			The <code>SimpleMDC</code> application illustrates how logback layouts, 
-			if configured appropriately, automatically output <code>MDC</code> information. 
-			Moreover, the information placed into the <code>MDC</code> can be used by 
-			multiple logger invocations.
+		<p>The <code>SimpleMDC</code> application illustrates how logback
+		layouts, if configured appropriately, can automatically output
+		<code>MDC</code> information.  Moreover, the information placed
+		into the <code>MDC</code> can be used by multiple logger
+		invocations.
 		</p>
 		
 		<h3>Advanced Use</h3>



More information about the logback-dev mailing list