Fix SLF4J-414

This commit is contained in:
Ceki Gulcu 2021-07-01 22:37:39 +02:00
parent 65eacb35a8
commit e74699276b
5 changed files with 49 additions and 8 deletions

View File

@ -141,6 +141,10 @@ public class BasicMDCAdapter implements MDCAdapter {
}
public void setContextMap(Map<String, String> contextMap) {
inheritableThreadLocal.set(new HashMap<String, String>(contextMap));
Map<String, String> copy = null;
if(contextMap != null) {
copy = new HashMap<String, String>(contextMap);
}
inheritableThreadLocal.set(copy);
}
}

View File

@ -83,6 +83,8 @@ public interface MDCAdapter {
* map and then copying the map passed as parameter. The context map
* parameter must only contain keys and values of type String.
*
* Implementations must support null valued map passed as parameter.
*
* @param contextMap must contain only keys and values of type String
*
* @since 1.5.1

View File

@ -42,8 +42,13 @@ import org.slf4j.spi.MDCAdapter;
* @author Lukasz Cwik
*/
public class BasicMDCAdapterTest {
MDCAdapter mdc = new BasicMDCAdapter();
protected MDCAdapter mdc = instantiateMDC();
protected MDCAdapter instantiateMDC() {
return new BasicMDCAdapter();
}
// leave MDC clean
@After
public void tearDown() throws Exception {
mdc.clear();
@ -103,6 +108,12 @@ public class BasicMDCAdapterTest {
assertNull(mdc.get("childKey"));
}
@Test
public void testInvokingSetContextMap_WithANullMap_SLF4J_414() {
mdc.setContextMap(null);
}
@Test
public void testMDCChildThreadCanOverwriteParentThread() throws Exception {
mdc.put("sharedKey", "parentValue");

View File

@ -25,7 +25,6 @@
package org.slf4j.log4j12;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.log4j.MDCFriend;
@ -39,6 +38,7 @@ public class Log4jMDCAdapter implements MDCAdapter {
}
}
@Override
public void clear() {
@SuppressWarnings("rawtypes")
Map map = org.apache.log4j.MDC.getContext();
@ -47,6 +47,7 @@ public class Log4jMDCAdapter implements MDCAdapter {
}
}
@Override
public String get(String key) {
return (String) org.apache.log4j.MDC.get(key);
}
@ -63,10 +64,12 @@ public class Log4jMDCAdapter implements MDCAdapter {
* @throws IllegalArgumentException
* in case the "key" or <b>"val"</b> parameter is null
*/
@Override
public void put(String key, String val) {
org.apache.log4j.MDC.put(key, val);
}
@Override
public void remove(String key) {
org.apache.log4j.MDC.remove(key);
}
@ -82,13 +85,21 @@ public class Log4jMDCAdapter implements MDCAdapter {
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void setContextMap(Map contextMap) {
@Override
public void setContextMap(Map<String, String> contextMap) {
Map old = org.apache.log4j.MDC.getContext();
// we must cater for the case where the contextMap argument is null
if(contextMap == null) {
if(old != null) {
old.clear();
}
return;
}
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());
for (Map.Entry<String, String> mapEntry : contextMap.entrySet()) {
org.apache.log4j.MDC.put(mapEntry.getKey(), mapEntry);
}
} else {
old.clear();

View File

@ -0,0 +1,13 @@
package org.slf4j.log4j12;
import org.slf4j.helpers.BasicMDCAdapterTest;
import org.slf4j.spi.MDCAdapter;
public class Log4jMDCAdapterTest extends BasicMDCAdapterTest {
protected MDCAdapter instantiateMDC() {
return new Log4jMDCAdapter();
}
}