Index: logback/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java =================================================================== --- logback/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java (revision 1677) +++ logback/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java (working copy) @@ -24,7 +24,15 @@ */ public class LogbackMDCAdapter implements MDCAdapter { - private final InheritableThreadLocal> inheritableThreadLocal = new InheritableThreadLocal>(); + private final InheritableThreadLocal> inheritableThreadLocal = new InheritableThreadLocal>() { + public final HashMap childValue(HashMap parentValue) { + if(parentValue != null) { + return new HashMap(parentValue); + } else { + return null; + } + } + }; LogbackMDCAdapter() { } @@ -38,12 +46,6 @@ * If the current thread does not have a context map it is created as a side * effect of this call. * - *

- * 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. - * * @throws IllegalArgumentException * in case the "key" parameter is null */ @@ -52,15 +54,14 @@ throw new IllegalArgumentException("key cannot be null"); } - HashMap oldMap = inheritableThreadLocal.get(); + HashMap hashMap = inheritableThreadLocal.get(); - HashMap newMap = new HashMap(); - if (oldMap != null) { - newMap.putAll(oldMap); + if (hashMap == null) { + hashMap=new HashMap() } - // the newMap replaces the old one for serialisation's sake - inheritableThreadLocal.set(newMap); - newMap.put(key, val); + + inheritableThreadLocal.set(hashMap); + hashMap.put(key, val); } /** @@ -81,23 +82,13 @@ /** * Remove the the context identified by the key parameter. - * - *

- * 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 oldMap = inheritableThreadLocal.get(); + HashMap hashMap = inheritableThreadLocal.get(); - HashMap newMap = new HashMap(); - if (oldMap != null) { - newMap.putAll(oldMap); + if (hashMap != null) { + hashMap.remove(key); } - // the newMap replaces the old one for serialisation's sake - inheritableThreadLocal.set(newMap); - newMap.remove(key); } /** @@ -113,11 +104,17 @@ } /** - * Get the current thread's MDC as a map. This method is intended to be used + * Get a copy of the current thread's MDC as a map. This method is intended to be used * internally. */ public Map getPropertyMap() { - return inheritableThreadLocal.get(); + HashMap hashMap = inheritableThreadLocal.get(); + + if(hashMap != null) { + return new HashMap(hashMap); + } else { + return null; + } } /**