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

ceki at slf4j.org ceki at slf4j.org
Mon Aug 20 21:25:37 CEST 2007


Author: ceki
Date: Mon Aug 20 21:25:37 2007
New Revision: 871

Modified:
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/MDC.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:
- fixing bug 58. In the MDC class, put, get and remove cannot take a null key. In, MDC.put(key, value), the value can be null only if the underlying MDC supports it. Log4j does not accept null values for the 'val' parameter, whereas logback does accept null for the 'val' parameter.

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	Mon Aug 20 21:25:37 2007
@@ -29,26 +29,25 @@
 import org.slf4j.spi.MDCAdapter;
 
 /**
- * This class hides and serves as a substitute for the underlying logging 
- * system's MDC implementation. 
+ * This class hides and serves as a substitute for the underlying logging
+ * system's MDC implementation.
  * 
  * <p>
- * 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.
+ * 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.
  * 
  * <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.
+ * 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.
  * 
  * <p>
- * For more information on MDC please see the 
- * <a href="http://logback.qos.ch/manual/mdc.html">chapter on 
- * MDC</a> in the logback manual.
+ * For more information on MDC please see the <a
+ * href="http://logback.qos.ch/manual/mdc.html">chapter on MDC</a> in the
+ * logback manual.
  * 
  * <p>
  * Please note that all methods in this class are static.
@@ -68,13 +67,15 @@
   static {
     try {
       mdcAdapter = StaticMDCBinder.SINGLETON.getMDCA();
-    } catch(NoClassDefFoundError ncde) {
+    } catch (NoClassDefFoundError ncde) {
       String msg = ncde.getMessage();
-      if(msg != null && msg.indexOf("org/slf4j/impl/StaticMDCBinder") != -1) {
-        Util.reportFailure("Failed to load class \"org.slf4j.impl.StaticMDCBinder\".");
-        Util.reportFailure("See "+NO_STATIC_MDC_BINDER_URL+" for further details.");
-        
-      } 
+      if (msg != null && msg.indexOf("org/slf4j/impl/StaticMDCBinder") != -1) {
+        Util
+            .reportFailure("Failed to load class \"org.slf4j.impl.StaticMDCBinder\".");
+        Util.reportFailure("See " + NO_STATIC_MDC_BINDER_URL
+            + " for further details.");
+
+      }
       throw ncde;
     } catch (Exception e) {
       // we should never get here
@@ -83,39 +84,64 @@
     }
   }
 
-  
   /**
    * Put a context value (the <code>val</code> parameter) as identified with
-   * the <code>key</code> parameter into the current thread's context map. This 
-   * method delegates all  work to the MDC of the underlying logging system.
+   * the <code>key</code> parameter into the current thread's context map.
+   * The <code>key</code> parameter cannot be null. The code>val</code> parameter 
+   * can be null only if the underlying implementation supports it.
+   * 
+   * <p>
+   * This method delegates all work to the MDC of the underlying logging system.
+   * 
+   * @throws IllegalArgumentException in case the "key" parameter is null
    */
-  public static void put(String key, String val) {
-    if(mdcAdapter == null) {
-      throw new IllegalStateException("MDCAdapter cannot be null. See also "+NULL_MDCA_URL);
+  public static void put(String key, String val) throws IllegalArgumentException {
+    if (key == null) {
+      throw new IllegalArgumentException("key parameter cannot be null");
+    }
+    if (mdcAdapter == null) {
+      throw new IllegalStateException("MDCAdapter cannot be null. See also "
+          + NULL_MDCA_URL);
     }
     mdcAdapter.put(key, val);
   }
 
   /**
-   * Get the context identified by the <code>key</code> parameter. 
-   * This method delegates all  work to the MDC of the underlying logging system.
+   * Get the context identified by the <code>key</code> parameter. The 
+   * <code>key</code> parameter cannot be null.
+   * 
+   * <p>This method delegates all work to the MDC of the underlying logging system. 
    * 
    * @return the string value identified by the <code>key</code> parameter.
+   * @throws IllegalArgumentException in case the "key" parameter is null
    */
-  public static String get(String key) {
-    if(mdcAdapter == null) {
-      throw new IllegalStateException("MDCAdapter cannot be null. See also "+NULL_MDCA_URL);
+  public static String get(String key) throws IllegalArgumentException {
+    if (key == null) {
+      throw new IllegalArgumentException("key parameter cannot be null");
+    }
+    
+    if (mdcAdapter == null) {
+      throw new IllegalStateException("MDCAdapter cannot be null. See also "
+          + NULL_MDCA_URL);
     }
     return mdcAdapter.get(key);
   }
 
   /**
-   * Remove the the context identified by the <code>key</code> parameter using 
-   * the underlying system's MDC implementation.
+   * Remove the the context identified by the <code>key</code> parameter using
+   * the underlying system's MDC implementation. The  <code>key</code> parameter 
+   * cannot be null.
+   * 
+   * @throws IllegalArgumentException in case the "key" parameter is null
    */
-  public static void remove(String key) {
-    if(mdcAdapter == null) {
-      throw new IllegalStateException("MDCAdapter cannot be null. See also "+NULL_MDCA_URL);
+  public static void remove(String key) throws IllegalArgumentException {
+    if (key == null) {
+      throw new IllegalArgumentException("key parameter cannot be null");
+    }
+    
+    if (mdcAdapter == null) {
+      throw new IllegalStateException("MDCAdapter cannot be null. See also "
+          + NULL_MDCA_URL);
     }
     mdcAdapter.remove(key);
   }
@@ -124,12 +150,13 @@
    * Clear all entries in the MDC of the underlying implementation.
    */
   public static void clear() {
-    if(mdcAdapter == null) {
-      throw new IllegalStateException("MDCAdapter cannot be null. See also "+NULL_MDCA_URL);
+    if (mdcAdapter == null) {
+      throw new IllegalStateException("MDCAdapter cannot be null. See also "
+          + NULL_MDCA_URL);
     }
     mdcAdapter.clear();
   }
-  
+
   /**
    * Returns the MDCAdapter instance currently in use.
    * 

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	Mon Aug 20 21:25:37 2007
@@ -11,7 +11,9 @@
 
   /**
    * Put a context value (the <code>val</code> parameter) as identified with
-   * the <code>key</code> parameter into the current thread's context map.
+   * the <code>key</code> parameter into the current thread's context map. 
+   * The <code>key</code> parameter cannot be null. The code>val</code> parameter 
+   * can be null only if the underlying implementation supports it.
    * 
    * <p>If the current thread does not have a context map it is created as a side
    * effect of this call.
@@ -20,13 +22,15 @@
 
   /**
    * Get the context identified by the <code>key</code> parameter.
+   * The <code>key</code> parameter cannot be null.
    * 
    * @return the string value identified by the <code>key</code> parameter.
    */
   public String get(String key);
 
   /**
-   * Remove the the context identified by the <code>key</code> parameter.
+   * Remove the the context identified by the <code>key</code> parameter. 
+   * The <code>key</code> parameter cannot be null.
    */
   public void remove(String key);
 

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	Mon Aug 20 21:25:37 2007
@@ -8,7 +8,7 @@
 
   public void clear() {
     Map map = org.apache.log4j.MDC.getContext();
-    if(map != null) {
+    if (map != null) {
       map.clear();
     }
   }
@@ -17,6 +17,18 @@
     return (String) org.apache.log4j.MDC.get(key);
   }
 
+  /**
+   * Put a context value (the <code>val</code> parameter) as identified with
+   * the <code>key</code> parameter into the current thread's context map. The
+   * <code>key</code> parameter cannot be null. Log4j does <em>not</em> 
+   * support null for the <code>val</code> parameter.
+   * 
+   * <p>
+   * This method delegates all work to log4j's MDC.
+   * 
+   * @throws IllegalArgumentException
+   *           in case the "key" or <b>"val"</b> parameter is null
+   */
   public void put(String key, String val) {
     org.apache.log4j.MDC.put(key, val);
   }



More information about the slf4j-dev mailing list