[slf4j-dev] svn commit: r1014 - in slf4j/trunk: slf4j-api/src/main/java/org/slf4j slf4j-api/src/main/java/org/slf4j/helpers slf4j-api/src/main/java/org/slf4j/spi slf4j-log4j12/src/main/java/org/slf4j/impl

ceki at slf4j.org ceki at slf4j.org
Tue May 27 19:39:47 CEST 2008


Author: ceki
Date: Tue May 27 19:39:46 2008
New Revision: 1014

Modified:
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/MDC.java
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/BasicMDCAdapter.java
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NOPMakerAdapter.java
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java
   slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jMDCAdapter.java

Log:
- Added a new method called getCopyOfPropertyMap to the MDCAdapter and MDC classes.
  This was requested in bug report 84.

  http://bugzilla.slf4j.org/show_bug.cgi?id=84


Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/MDC.java
==============================================================================
--- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/MDC.java	(original)
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/MDC.java	Tue May 27 19:39:46 2008
@@ -24,6 +24,9 @@
 
 package org.slf4j;
 
+import java.util.Map;
+
+import org.slf4j.helpers.BasicMDCAdapter;
 import org.slf4j.helpers.Util;
 import org.slf4j.impl.StaticMDCBinder;
 import org.slf4j.spi.MDCAdapter;
@@ -36,13 +39,13 @@
  * If the underlying logging system offers MDC functionality, then SLF4J's MDC,
  * i.e. this class, will delegate to the underlying system's MDC. Note that at
  * this time, only two logging systems, namely log4j and logback, offer MDC
- * functionality. If the undelying system does not support MDC, then SLF4J will
- * silently drop MDC information.
+ * functionality. If the underlying system does not support MDC, e.g. java.util.logging, 
+ * then SLF4J will use a {@link BasicMDCAdapter}. 
  * 
  * <p>
  * Thus, as a SLF4J user, you can take advantage of MDC in the presence of log4j
- * or logback, but without forcing log4j or logback as dependencies upon your
- * users.
+ * logback, or java.util.logging, but without forcing these systems as dependencies 
+ * upon your users.
  * 
  * <p>
  * For more information on MDC please see the <a
@@ -147,7 +150,7 @@
     mdcAdapter.remove(key);
   }
 
-  /**
+  /** 
    * Clear all entries in the MDC of the underlying implementation.
    */
   public static void clear() {
@@ -157,7 +160,22 @@
     }
     mdcAdapter.clear();
   }
-
+  
+  /**
+   * Return a copy of the current thread's context map, with keys and 
+   * values of type String. Returned value may be null.
+   * 
+   * @return A copy of the current thread's context map. May be null.
+   * @since 1.5.1
+   */
+  public static Map getCopyOfPropertyMap() {
+    if (mdcAdapter == null) {
+      throw new IllegalStateException("MDCAdapter cannot be null. See also "
+          + NULL_MDCA_URL);
+    }
+    return mdcAdapter.getCopyOfPropertyMap();
+  }
+  
   /**
    * Returns the MDCAdapter instance currently in use.
    * 
@@ -167,4 +185,6 @@
   public static MDCAdapter getMDCAdapter() {
     return mdcAdapter;
   }
+  
+  
 }
\ No newline at end of file

Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/BasicMDCAdapter.java
==============================================================================
--- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/BasicMDCAdapter.java	(original)
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/BasicMDCAdapter.java	Tue May 27 19:39:46 2008
@@ -23,100 +23,112 @@
  */
 package org.slf4j.helpers;
 
-
 import org.slf4j.spi.MDCAdapter;
 
 import java.util.HashMap;
+import java.util.Map;
 import java.util.Set;
 
+/**
+ * Basic MDC implementation, which can be used with logging systems that lack
+ * out-of-the-box MDC support.
+ * 
+ * This code is largely based on logback's <a
+ * href="http://svn.qos.ch/viewvc/logback/trunk/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java">
+ * LogbackMDCAdapter</a>.
+ * 
+ * @author Ceki Gulcu
+ * @author Maarten Bosteels
+ * 
+ * @since 1.5.0
+ */
+public class BasicMDCAdapter implements MDCAdapter {
+
+  private InheritableThreadLocal inheritableThreadLocal = new InheritableThreadLocal();
+
   /**
-   * Basic MDC implementation, which can be used with logging systems that
-   * lack out-of-the-box MDC support.
+   * Put a context value (the <code>val</code> parameter) as identified with
+   * the <code>key</code> parameter into the current thread's context map.
+   * Note that contrary to log4j, the <code>val</code> parameter can be null.
    * 
-   * This code is largely based on logback's <a href="http://svn.qos.ch/viewvc/logback/trunk/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java">
-   * LogbackMDCAdapter</a>.
+   * <p>
+   * If the current thread does not have a context map it is created as a side
+   * effect of this call.
    * 
-   * @author Ceki Gulcu
-   * @author Maarten Bosteels
-   * 
-   * @since 1.5.0
+   * @throws IllegalArgumentException
+   *                 in case the "key" parameter is null
    */
-  public class BasicMDCAdapter implements MDCAdapter {
+  public void put(String key, String val) {
+    if (key == null) {
+      throw new IllegalArgumentException("key cannot be null");
+    }
+    HashMap map = (HashMap) inheritableThreadLocal.get();
+    if (map == null) {
+      map = new HashMap();
+      inheritableThreadLocal.set(map);
+    }
+    map.put(key, val);
+  }
 
-    private InheritableThreadLocal inheritableThreadLocal = new InheritableThreadLocal();
+  /**
+   * Get the context identified by the <code>key</code> parameter.
+   */
+  public String get(String key) {
+    HashMap hashMap = (HashMap) inheritableThreadLocal.get();
+    if ((hashMap != null) && (key != null)) {
+      return (String) hashMap.get(key);
+    } else {
+      return null;
+    }
+  }
 
-    /**
-     * Put a context value (the <code>val</code> parameter) as identified with
-     * the <code>key</code> parameter into the current thread's context map.
-     * Note that contrary to log4j, the <code>val</code> parameter can be null.
-     * 
-     * <p>
-     * If the current thread does not have a context map it is created as a side
-     * effect of this call.
-
-     * @throws IllegalArgumentException
-     *           in case the "key" parameter is null
-     */
-    public void put(String key, String val) {
-      if (key == null) {
-        throw new IllegalArgumentException("key cannot be null");
-      }
-      HashMap map = (HashMap) inheritableThreadLocal.get();
-      if (map == null) {
-        map = new HashMap();
-        inheritableThreadLocal.set(map);
-      }
-      map.put(key, val);
-    }
-
-    /**
-     * Get the context identified by the <code>key</code> parameter.
-     */
-    public String get(String key) {
-      HashMap hashMap = (HashMap) inheritableThreadLocal.get();
-      if ((hashMap != null) && (key != null)) {
-        return (String) hashMap.get(key);
-      } else {
-        return null;
-      }
-    }
-
-
-    /**
-     * Remove the the context identified by the <code>key</code> parameter.
-     */
-    public void remove(String key) {
-      HashMap map = (HashMap) inheritableThreadLocal.get();
-      if (map != null) {
-        map.remove(key);
-      }
-    }
-
-    /**
-     * Clear all entries in the MDC.
-     */
-    public void clear() {
-      HashMap hashMap = (HashMap) inheritableThreadLocal.get();
-      if (hashMap != null) {
-        hashMap.clear();
-        inheritableThreadLocal.remove();
-      }
-    }
-
-    /**
-     * Returns the keys in the MDC as a {@link Set} of {@link String}s
-     * The returned value can be null.
-     * @return the keys in the MDC
-     */
-    public Set getKeys() {
-      HashMap hashMap = (HashMap) inheritableThreadLocal.get();
-      if (hashMap != null) {
-        return hashMap.keySet();
-      } else {
-        return null;
-      }
+  /**
+   * Remove the the context identified by the <code>key</code> parameter.
+   */
+  public void remove(String key) {
+    HashMap map = (HashMap) inheritableThreadLocal.get();
+    if (map != null) {
+      map.remove(key);
+    }
+  }
+
+  /**
+   * Clear all entries in the MDC.
+   */
+  public void clear() {
+    HashMap hashMap = (HashMap) inheritableThreadLocal.get();
+    if (hashMap != null) {
+      hashMap.clear();
+      inheritableThreadLocal.remove();
+    }
+  }
+
+  /**
+   * Returns the keys in the MDC as a {@link Set} of {@link String}s The
+   * returned value can be null.
+   * 
+   * @return the keys in the MDC
+   */
+  public Set getKeys() {
+    HashMap hashMap = (HashMap) inheritableThreadLocal.get();
+    if (hashMap != null) {
+      return hashMap.keySet();
+    } else {
+      return null;
+    }
+  }
+  /**
+   * Return a copy of the current thread's context map. 
+   * Returned value may be null.
+   * 
+   */
+  public Map getCopyOfPropertyMap() {
+    HashMap hashMap = (HashMap) inheritableThreadLocal.get();
+    if (hashMap != null) {
+      return new HashMap(hashMap);
+    } else {
+      return null;
     }
+  }
 
-  
-  
 }

Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NOPMakerAdapter.java
==============================================================================
--- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NOPMakerAdapter.java	(original)
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NOPMakerAdapter.java	Tue May 27 19:39:46 2008
@@ -1,5 +1,7 @@
 package org.slf4j.helpers;
 
+import java.util.Map;
+
 import org.slf4j.spi.MDCAdapter;
 
 /**
@@ -26,4 +28,8 @@
   public void remove(String key) {
   }
 
+  public Map getCopyOfPropertyMap() {
+    return null;
+  }
+
 }

Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java
==============================================================================
--- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java	(original)
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java	Tue May 27 19:39:46 2008
@@ -24,6 +24,8 @@
 
 package org.slf4j.spi;
 
+import java.util.Map;
+
 /**
  * This interface abstracts the service offered by various MDC
  * implementations.
@@ -67,4 +69,12 @@
    */
   public void clear();
 
+  /**
+   * Return a copy of the current thread's context map, with keys and 
+   * values of type String. Returned value may be null.
+   * 
+   * @return A copy of the current thread's context map. May be null.
+   * @since 1.5.1
+   */
+  public Map getCopyOfPropertyMap();
 }

Modified: slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jMDCAdapter.java
==============================================================================
--- slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jMDCAdapter.java	(original)
+++ slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jMDCAdapter.java	Tue May 27 19:39:46 2008
@@ -1,5 +1,6 @@
 package org.slf4j.impl;
 
+import java.util.HashMap;
 import java.util.Map;
 
 import org.slf4j.spi.MDCAdapter;
@@ -37,4 +38,14 @@
     org.apache.log4j.MDC.remove(key);
   }
 
+  public Map getCopyOfPropertyMap() {
+    Map old = org.apache.log4j.MDC.getContext();
+    if(old != null) {
+      return new HashMap(old);
+    } else {
+      return null;
+    }
+  }
+
+  
 }



More information about the slf4j-dev mailing list