I do not see a direct way to change this. Two alternatives that come to mind are the following:
Method number 1: Wrap all Runnables
Imagine an abstract class that will copy MDCfrom the original Threadto the new Threadand 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 Runnablecomes from an external API that you cannot change this code, use a wraphelper 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.