[slf4j-dev] svn commit: r1042 - in slf4j/trunk: jul-to-slf4j/src/main/java/org/slf4j/bridge slf4j-api 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 slf4j-log4j12/src/test/java/org/slf4j
ceki at slf4j.org
ceki at slf4j.org
Tue Jun 3 21:40:50 CEST 2008
Author: ceki
Date: Tue Jun 3 21:40:50 2008
New Revision: 1042
Modified:
slf4j/trunk/jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java
slf4j/trunk/slf4j-api/pom.xml
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
slf4j/trunk/slf4j-log4j12/src/test/java/org/slf4j/InvocationTest.java
Log:
- added the setContextMap(Map) method to MDCAdapter interface as well as the MDC class
as requested in bug 84.
- minor changes to SLF4JBridgeHandler
- added clirr plugin to slf4j-api module
Modified: slf4j/trunk/jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java
==============================================================================
--- slf4j/trunk/jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java (original)
+++ slf4j/trunk/jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java Tue Jun 3 21:40:50 2008
@@ -44,7 +44,32 @@
// Based on http://bugzilla.slf4j.org/show_bug.cgi?id=38
/**
- * JUL bridge/router for SLF4J.
+ * Bridge/route all JUL log records to the SLF4J API.
+ *
+ * <p>Essentially, the idea is to install on the root logger an instance of
+ * SLF4JBridgeHandler as the sole JUL handler in the system. Subsequently, the
+ * SLF4JBridgeHandler instance will redirect all JUL log records are redirected to
+ * the SLF4J API based on the following mapping of levels:
+ *
+ * <pre>
+ * FINEST -> TRACE
+ * FINER -> DEBUG
+ * FINE -> DEBUG
+ * INFO -> INFO
+ * WARNING -> WARN
+ * SEVER -> ERROR
+ * </pre>
+ *
+ * Usage:
+ *
+ * <pre>
+ * // once during initialization time of your application
+ * SLF4JHandler.install(handler);
+ *
+ * // usual pattern: get a Logger and then log a message
+ * java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger("org.wombat");
+ * julLogger.fine("hello world"); // this will get redirected to SLF4J
+ * </pre>
*
* @author Christian Stein
* @author Joern Huxhorn
@@ -118,7 +143,7 @@
return LoggerFactory.getLogger(name);
}
- public void callLocationAwareLogger(LocationAwareLogger lal, LogRecord record) {
+ protected void callLocationAwareLogger(LocationAwareLogger lal, LogRecord record) {
int julLevelValue = record.getLevel().intValue();
int slf4jLevel;
@@ -136,7 +161,7 @@
lal.log(null, FQCN, slf4jLevel, record.getMessage(), record.getThrown());
}
- public void callPlainSLF4JLogger(Logger slf4jLogger, LogRecord record) {
+ protected void callPlainSLF4JLogger(Logger slf4jLogger, LogRecord record) {
int julLevelValue = record.getLevel().intValue();
if (julLevelValue <= TRACE_LEVEL_THRESHOLD) {
slf4jLogger.trace(record.getMessage(), record.getThrown());
@@ -165,9 +190,7 @@
* ignored and is not published.
*/
public void publish(LogRecord record) {
- /*
- * Silently ignore null records.
- */
+ // Silently ignore null records.
if (record == null) {
return;
}
@@ -183,5 +206,4 @@
callPlainSLF4JLogger(slf4jLogger, record);
}
}
-
}
Modified: slf4j/trunk/slf4j-api/pom.xml
==============================================================================
--- slf4j/trunk/slf4j-api/pom.xml (original)
+++ slf4j/trunk/slf4j-api/pom.xml Tue Jun 3 21:40:50 2008
@@ -20,10 +20,9 @@
<dependencies>
- </dependencies>
+ </dependencies>
<build>
-
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@@ -86,7 +85,17 @@
</plugins>
</build>
-
-
+
+ <reporting>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>clirr-maven-plugin</artifactId>
+ <configuration>
+ <comparisonVersion>1.5.0</comparisonVersion>
+ </configuration>
+ </plugin>
+ </plugins>
+ </reporting>
</project>
\ No newline at end of file
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 Jun 3 21:40:50 2008
@@ -175,6 +175,23 @@
}
return mdcAdapter.getCopyOfContextMap();
}
+
+ /**
+ * Set the current thread's context map by first clearing any existing
+ * map and then copying the map passed as parameter. The context map passed
+ * as parameter must only contain keys and values of type String.
+ *
+ * @param contextMap must contain only keys and values of type String
+ * @since 1.5.1
+ */
+ public static void setContextMap(Map contextMap) {
+ if (mdcAdapter == null) {
+ throw new IllegalStateException("MDCAdapter cannot be null. See also "
+ + NULL_MDCA_URL);
+ }
+ mdcAdapter.setContextMap(contextMap);
+ }
+
/**
* Returns the MDCAdapter instance currently in use.
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 Jun 3 21:40:50 2008
@@ -131,4 +131,15 @@
}
}
+ public void setContextMap(Map contextMap) {
+ HashMap hashMap = (HashMap) inheritableThreadLocal.get();
+ if (hashMap != null) {
+ hashMap.clear();
+ hashMap.putAll(contextMap);
+ } else {
+ hashMap = new HashMap(contextMap);
+ inheritableThreadLocal.set(hashMap);
+ }
+ }
+
}
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 Jun 3 21:40:50 2008
@@ -32,4 +32,8 @@
return null;
}
+ public void setContextMap(Map contextMap) {
+ // NOP
+ }
+
}
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 Jun 3 21:40:50 2008
@@ -77,4 +77,15 @@
* @since 1.5.1
*/
public Map getCopyOfContextMap();
+
+ /**
+ * Set the current thread's context map by first clearing any existing
+ * map and then copying the map passed as parameter. The context map
+ * parameter must only contain keys and values of type String.
+ *
+ * @param contextMap must contain only keys and values of type String
+ *
+ * @since 1.5.1
+ */
+ public void setContextMap(Map contextMap);
}
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 Jun 3 21:40:50 2008
@@ -1,6 +1,7 @@
package org.slf4j.impl;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.Map;
import org.slf4j.spi.MDCAdapter;
@@ -47,5 +48,17 @@
}
}
-
+ public void setContextMap(Map contextMap) {
+ Map old = org.apache.log4j.MDC.getContext();
+ if(old == null) {
+ Iterator entrySetIterator = contextMap.entrySet().iterator();
+ while(entrySetIterator.hasNext()) {
+ Map.Entry mapEntry = (Map.Entry) entrySetIterator.next();
+ org.apache.log4j.MDC.put((String) mapEntry.getKey(), mapEntry.getValue());
+ }
+ } else {
+ old.clear();
+ old.putAll(contextMap);
+ }
+ }
}
Modified: slf4j/trunk/slf4j-log4j12/src/test/java/org/slf4j/InvocationTest.java
==============================================================================
--- slf4j/trunk/slf4j-log4j12/src/test/java/org/slf4j/InvocationTest.java (original)
+++ slf4j/trunk/slf4j-log4j12/src/test/java/org/slf4j/InvocationTest.java Tue Jun 3 21:40:50 2008
@@ -33,6 +33,9 @@
package org.slf4j;
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.log4j.spi.LoggingEvent;
import junit.framework.TestCase;
@@ -163,4 +166,18 @@
} catch (IllegalArgumentException e) {
}
}
+
+ public void testMDCContextMapValues() {
+ Map map = new HashMap();
+ map.put("ka", "va");
+ map.put("kb", "vb");
+
+ MDC.put("k", "v");
+ assertEquals("v", MDC.get("k"));
+ MDC.setContextMap(map);
+ assertNull(MDC.get("k"));
+ assertEquals("va", MDC.get("ka"));
+ assertEquals("vb", MDC.get("kb"));
+ }
+
}
More information about the slf4j-dev
mailing list