I do not see a direct way to change this. Two alternatives that come to mind are the following:
Method number 1: Wrap all Runnable
s
Imagine an abstract class that will copy MDC
from the original Thread
to the new Thread
and use it insteadRunnable
public abstract class MdcAwareRunnable implements Runnable
{
private Map<String, String> originalMdc;
public MdcAwareRunnable()
{
this.originalMdc = MDC.getCopyOfContextMap();
}
@Override
public void run()
{
MDC.setContextMap(originalMdc);
runImpl();
}
protected abstract void runImpl();
public static MdcAwareRunnable wrap(Runnable runnable)
{
return new MdcAwareRunnable()
{
@Override
protected void runImpl()
{
runnable.run();
}
};
}
}
If some Runnable
comes from an external API that you cannot change this code, use a wrap
helper method.
Disadvantage: you need to analyze and modify the entire code.
Method number 2: mess with internal components slf4j
LogbackMDCAdapter
, InheritableThreadLocal
- . - , MDC.mdcAdapter . , , , №1.
. LogbackMDCAdapter
. . LoggingEvent.java LogbackMDCAdapter.getPropertyMap
.
№ 3: ( )
, .
LogbackMDCAdapter
, , .class logback.jar.
LogbackMDCAdapter
, .class org.slf4j.impl.StaticMDCBinder
logback.jar , LogbackMDCAdapter
logback.jar, . MDC
, MDCAdapter
.
ClassLoader
, org.slf4j.impl.StaticMDCBinder
, logback.jar. : , , -, ClassLoaders.