[LOGBack-dev] svn commit: r523 - in logback/trunk: . logback-classic/src/main/java/ch/qos/logback/classic logback-classic/src/test/java/ch/qos/logback/classic logback-core/src/main/java/ch/qos/logback/core/rolling logback-core/src/main/java/ch/qos/logback/core/rolling/helper logback-site/src/site/fml

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Sep 7 21:07:49 CEST 2006


Author: ceki
Date: Thu Sep  7 21:07:49 2006
New Revision: 523

Added:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/MDC.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/MDCTest.java
   logback/trunk/logback-site/src/site/fml/
   logback/trunk/logback-site/src/site/fml/codes.fml
Modified:
   logback/trunk/   (props changed)
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/RollingCalendar.java

Log:
Added MDC support
Minor bug fixes

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/MDC.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/MDC.java	Thu Sep  7 21:07:49 2006
@@ -0,0 +1,105 @@
+package ch.qos.logback.classic;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * A <em>Mapped Diagnostic Context</em>, or MDC in short, is an instrument for
+ * distinguishing interleaved log output from different sources. Log output is
+ * typically interleaved when a server handles multiple clients
+ * near-simultaneously.
+ * <p>
+ * <b><em>The MDC is managed on a per thread basis</em></b>. A child thread
+ * automatically inherits a <em>copy</em> of the mapped diagnostic context of
+ * its parent.
+ * <p>
+ * 
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class MDC {
+  private static final ThreadLocal<HashMap<String, String>> threadLocal = new ThreadLocal<HashMap<String, String>>();
+
+  private MDC() {
+  }
+
+  /**
+   * Put a context value (the <code>val</code> parameter) as identified with
+   * the <code>key</code> parameter into the current thread's context map.
+   * 
+   * <p>
+   * If the current thread does not have a context map it is created as a side
+   * effect of this call.
+   */
+  public static void put(String key, String val) {
+    HashMap<String, String> hashMap = threadLocal.get();
+
+    if (hashMap == null) {
+      hashMap = new HashMap<String, String>();
+      threadLocal.set(hashMap);
+    }
+
+    hashMap.put(key, val);
+  }
+
+  /**
+   * Get the context identified by the <code>key</code> parameter.
+   * 
+   * <p>
+   * This method has no side effects.
+   */
+  public static String get(String key) {
+    HashMap<String, String> hashMap = threadLocal.get();
+
+    if ((hashMap != null) && (key != null)) {
+      return hashMap.get(key);
+    } else {
+      return null;
+    }
+  }
+
+  /**
+   * Remove the the context identified by the <code>key</code> parameter.
+   */
+  public static void remove(String key) {
+    HashMap<String, String> hashMap = threadLocal.get();
+
+    if (hashMap != null) {
+      hashMap.remove(key);
+    }
+  }
+
+  /**
+   * Clear all entries in the MDC.
+   */
+  public static void clear() {
+    HashMap<String, String> hashMap = threadLocal.get();
+
+    if (hashMap != null) {
+      hashMap.clear();
+      threadLocal.remove();
+    }
+  }
+
+  /**
+   * Get the current thread's MDC as a map. This method is intended to be used
+   * internally.
+   */
+  public static Map<String, String> getContext() {
+    return threadLocal.get();
+  }
+
+  /**
+   * Returns the keys in the MDC as a {@link Set}. The returned value
+   * can be null.
+   */
+  public static Set<String> getKeys() {
+    HashMap<String, String> hashMap = threadLocal.get();
+
+    if (hashMap != null) {
+      return hashMap.keySet();
+    } else {
+      return null;
+    }
+  }
+}

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/MDCTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/MDCTest.java	Thu Sep  7 21:07:49 2006
@@ -0,0 +1,49 @@
+package ch.qos.logback.classic;
+
+import junit.framework.TestCase;
+
+public class MDCTest extends TestCase {
+
+
+  public void test() throws InterruptedException {
+    TestThread threadA = new TestThread("a");
+    threadA.start();
+    
+    TestThread threadB = new TestThread("b");
+    threadB.start();
+    
+    threadA.join();
+    threadB.join();
+    
+    
+    assertNull(threadA.x0);
+    assertEquals("a", threadA.x1);
+    assertNull(threadA.x2);
+    
+    assertNull(threadB.x0);
+    assertEquals("b", threadB.x1);
+    assertNull(threadB.x2);
+    
+  }
+  
+}
+
+class TestThread extends Thread {
+  
+  String val;
+  TestThread(String val) {
+    this.val = val;
+  }
+  String x0;
+  String x1;
+  String x2;
+  
+  public void run() {
+    x0 = MDC.get("x");
+    MDC.put("x", val);
+    x1 = MDC.get("x");
+    MDC.clear();
+    x2 = MDC.get("x");
+    System.out.println("Exiting "+val);
+  }
+}
\ No newline at end of file

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java	Thu Sep  7 21:07:49 2006
@@ -123,7 +123,7 @@
   static final String FNP_NOT_SET =
     "The FileNamePattern option must be set before using TimeBasedRollingPolicy. ";
   static final String SEE_FNP_NOT_SET =
-    "See also http://www.logback.com/doc/codes.html#tbr_fnp_not_set";
+    "See also http://logback.qos.ch/doc/codes.html#tbr_fnp_not_set";
   RollingCalendar rc;
   long nextCheck;
   Date lastCheck = new Date();

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/RollingCalendar.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/RollingCalendar.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/RollingCalendar.java	Thu Sep  7 21:07:49 2006
@@ -201,6 +201,7 @@
     case TOP_OF_WEEK:
       this.set(Calendar.DAY_OF_WEEK, getFirstDayOfWeek());
       this.set(Calendar.HOUR_OF_DAY, 0);
+      this.set(Calendar.MINUTE, 0);
       this.set(Calendar.SECOND, 0);
       this.set(Calendar.MILLISECOND, 0);
       this.add(Calendar.WEEK_OF_YEAR, 1);
@@ -210,6 +211,7 @@
     case TOP_OF_MONTH:
       this.set(Calendar.DATE, 1);
       this.set(Calendar.HOUR_OF_DAY, 0);
+      this.set(Calendar.MINUTE, 0);
       this.set(Calendar.SECOND, 0);
       this.set(Calendar.MILLISECOND, 0);
       this.add(Calendar.MONTH, 1);

Added: logback/trunk/logback-site/src/site/fml/codes.fml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-site/src/site/fml/codes.fml	Thu Sep  7 21:07:49 2006
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="iso-8859-1"?>
+
+<document>
+
+  <body>
+
+  <faqs title="Logback error messages and their meanings">
+      
+  <part id="Generalities">
+    <title>Generalities</title>
+
+    <faq id="tbr_fnp_not_set">
+
+      <question>The <b>FileNamePattern</b> option must be set before
+      using <code>TimeBasedRollingPolicy</code> or
+      <code>FixedWindowRollingPolicy</code>.
+      </question>
+      
+      <answer>
+        <p>The <b>FileNamePattern</b> option for both
+        <code>TimeBasedRollingPolicy</code> and
+        <code>FixedWindowRollingPolicy</code> is mandatory.
+        </p>
+      </answer>
+
+    </faq>
+    </part>
+    </faqs>
+  </body>
+</document>



More information about the logback-dev mailing list