[logback-dev] svn commit: r1550 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic main/java/ch/qos/logback/classic/net main/java/ch/qos/logback/classic/spi main/java/ch/qos/logback/classic/turbo main/java/org/slf4j/impl test/java/ch/qos/logback/classic test/java/ch/qos/logback/classic/net test/java/ch/qos/logback/classic/pattern test/java/ch/qos/logback/classic/turbo

noreply.ceki at qos.ch noreply.ceki at qos.ch
Mon Jul 9 22:09:49 CEST 2007


Author: ceki
Date: Mon Jul  9 22:09:49 2007
New Revision: 1550

Added:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketAcceptor.java
   logback/trunk/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java
   logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticMDCBinder.java
Removed:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/MDC.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCFilter.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/MDCTestThread.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/LoggingEventSerializationTest.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTest.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/MDCConverterTest.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/turbo/DebugUsersTurboFilter.java

Log:

- realigning with MDC as defined in SLF4J

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketAcceptor.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketAcceptor.java	Mon Jul  9 22:09:49 2007
@@ -0,0 +1,10 @@
+package ch.qos.logback.classic.net;
+
+class SocketAcceptor extends Thread {
+
+  
+  @Override
+  public void run() {
+    
+  }
+}

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java	Mon Jul  9 22:09:49 2007
@@ -16,12 +16,13 @@
 import java.io.Serializable;
 import java.util.Map;
 
+import org.slf4j.MDC;
 import org.slf4j.Marker;
 import org.slf4j.helpers.MessageFormatter;
+import org.slf4j.impl.LogbackMDCAdapter;
 
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;
-import ch.qos.logback.classic.MDC;
 
 /**
  * The internal representation of logging events. When an affirmative decision
@@ -117,7 +118,9 @@
     }
     timeStamp = System.currentTimeMillis();
 
-    mdcPropertyMap = MDC.getPropertyMap();
+    // the case is ugly but under the circumstances acceptable
+    LogbackMDCAdapter logbackMDCAdapter = (LogbackMDCAdapter) MDC.getMDCAdapter();
+    mdcPropertyMap = logbackMDCAdapter.getPropertyMap();
   }
 
   public void setArgumentArray(Object[] argArray) {

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCFilter.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCFilter.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCFilter.java	Mon Jul  9 22:09:49 2007
@@ -1,10 +1,10 @@
 package ch.qos.logback.classic.turbo;
 
+import org.slf4j.MDC;
 import org.slf4j.Marker;
 
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;
-import ch.qos.logback.classic.MDC;
 import ch.qos.logback.core.spi.FilterReply;
 
 /**

Added: logback/trunk/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java	Mon Jul  9 22:09:49 2007
@@ -0,0 +1,128 @@
+package org.slf4j.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.slf4j.spi.MDCAdapter;
+
+/**
+ * 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>
+ * 
+ * For more information about MDC, please refer to the online manual at
+ * http://logback.qos.ch/manual/mdc.html
+ * 
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class LogbackMDCAdapter implements MDCAdapter{
+  
+  private final ThreadLocal<HashMap<String, String>> threadLocal = new ThreadLocal<HashMap<String, String>>();
+
+  LogbackMDCAdapter() {
+  }
+
+  /**
+   * 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.
+   * 
+   * <p>
+   * Each time a value is added, a new instance of the map is created. This is
+   * to be certain that the serialization process will operate on the updated map
+   * and not send a reference to the old map, thus not allowing the remote logback
+   * component to see the latest changes.
+   */
+  public void put(String key, String val) {
+    HashMap<String, String> oldMap = threadLocal.get();
+
+    HashMap<String, String> newMap = new HashMap<String, String>();
+    if (oldMap != null) {
+    	newMap.putAll(oldMap);
+    }
+    // the newMap replaces the old one for serialisation's sake
+    threadLocal.set(newMap);
+    newMap.put(key, val);
+  }
+
+  /**
+   * Get the context identified by the <code>key</code> parameter.
+   * 
+   * <p>
+   * This method has no side effects.
+   */
+  public 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.
+   * 
+   * <p>
+   * Each time a value is removed, a new instance of the map is created. This is
+   * to be certain that the serialization process will operate on the updated map
+   * and not send a reference to the old map, thus not allowing the remote logback
+   * component to see the latest changes.
+   */
+  public void remove(String key) {
+    HashMap<String, String> oldMap = threadLocal.get();
+
+    HashMap<String, String> newMap = new HashMap<String, String>();
+    if (oldMap != null) {
+    	newMap.putAll(oldMap);
+    }
+    // the newMap replaces the old one for serialisation's sake
+    threadLocal.set(newMap);
+    newMap.remove(key);
+  }
+
+  /**
+   * Clear all entries in the MDC.
+   */
+  public 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 Map<String, String> getPropertyMap() {
+    return threadLocal.get();
+  }
+
+  /**
+   * Returns the keys in the MDC as a {@link Set}. The returned value
+   * can be null.
+   */
+  public Set<String> getKeys() {
+    HashMap<String, String> hashMap = threadLocal.get();
+
+    if (hashMap != null) {
+      return hashMap.keySet();
+    } else {
+      return null;
+    }
+  }
+}

Added: logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticMDCBinder.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticMDCBinder.java	Mon Jul  9 22:09:49 2007
@@ -0,0 +1,33 @@
+package org.slf4j.impl;
+
+import org.slf4j.spi.MDCAdapter;
+
+
+/**
+ * This implementation is bound to {@link LogbackMDCAdapter}.
+ *
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class StaticMDCBinder {
+
+  
+  /**
+   * The unique instance of this class.
+   */
+  public static final StaticMDCBinder SINGLETON = new StaticMDCBinder();
+
+  private StaticMDCBinder() {
+  }
+  
+  /**
+   * Currently this method always returns an instance of 
+   * {@link StaticMDCBinder}.
+   */
+  public MDCAdapter getMDCA() {
+     return new LogbackMDCAdapter();
+  }
+  
+  public String  getMDCAdapterClassStr() {
+    return LogbackMDCAdapter.class.getName();
+  }
+}

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/MDCTestThread.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/MDCTestThread.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/MDCTestThread.java	Mon Jul  9 22:09:49 2007
@@ -1,5 +1,7 @@
 package ch.qos.logback.classic;
 
+import org.slf4j.MDC;
+
 public class MDCTestThread extends Thread {
   
   String val;

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/LoggingEventSerializationTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/LoggingEventSerializationTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/LoggingEventSerializationTest.java	Mon Jul  9 22:09:49 2007
@@ -7,10 +7,12 @@
 import java.util.Map;
 
 import junit.framework.TestCase;
+
+import org.slf4j.MDC;
+
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;
 import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.MDC;
 import ch.qos.logback.classic.spi.LoggerContextRemoteView;
 import ch.qos.logback.classic.spi.LoggerRemoteView;
 import ch.qos.logback.classic.spi.LoggingEvent;

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTest.java	Mon Jul  9 22:09:49 2007
@@ -13,13 +13,13 @@
 
 import junit.framework.TestCase;
 
+import org.slf4j.MDC;
 import org.slf4j.Marker;
 import org.slf4j.MarkerFactory;
 
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;
 import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.MDC;
 import ch.qos.logback.classic.net.mock.MockSocketServer;
 import ch.qos.logback.classic.spi.LoggerContextRemoteView;
 import ch.qos.logback.classic.spi.LoggerRemoteView;

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java	Mon Jul  9 22:09:49 2007
@@ -14,12 +14,12 @@
 
 import junit.framework.TestCase;
 
+import org.slf4j.MDC;
 import org.slf4j.MarkerFactory;
 
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;
 import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.MDC;
 import ch.qos.logback.classic.spi.LoggingEvent;
 import ch.qos.logback.core.CoreGlobal;
 import ch.qos.logback.core.net.SyslogConstants;

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/MDCConverterTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/MDCConverterTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/MDCConverterTest.java	Mon Jul  9 22:09:49 2007
@@ -1,9 +1,11 @@
 package ch.qos.logback.classic.pattern;
 
 import junit.framework.TestCase;
+
+import org.slf4j.MDC;
+
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.MDC;
 import ch.qos.logback.classic.spi.LoggingEvent;
 
 public class MDCConverterTest extends TestCase {

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/turbo/DebugUsersTurboFilter.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/turbo/DebugUsersTurboFilter.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/turbo/DebugUsersTurboFilter.java	Mon Jul  9 22:09:49 2007
@@ -3,12 +3,12 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.slf4j.MDC;
 import org.slf4j.Marker;
 
 import ch.qos.logback.classic.ClassicGlobal;
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;
-import ch.qos.logback.classic.MDC;
 import ch.qos.logback.core.spi.FilterReply;
 
 /**



More information about the logback-dev mailing list