Description
Currently we have two bridges between MDC
and ThreadContext
:
MDCContextMap
inlog4j-to-slf4j
that forwardsThreadContext
calls toMDC
,Log4jMDCAdapter
inlog4j-slf4j2-impl
andlog4j-slf4j-impl
that forwardsMDC
calls toThreadContext
.
While the mutator methods of these bridges are required by user code, the accessors are basically useless:
- Logback does not call any
ThreadContext
accessor, - Log4j Core does not call any
MDC
accessor.
The accessors are only used by third-party integrators like the Context Propagation Library (see Slf4jThreadLocalAccessor
for example).
In view of a future integration with context-propagation
I would propose to:
- modify
MDCContextMap
accessors to always returnnull
in theget
andgetImmutableMapOrNull
and return a newHashMap
in thegetCopy
method. These values are allowed by theThreadContextMap
contract, - modify
Log4jMDCAdapter
accessors to always returnnull
. This value is allowed by theMDCAdapter
contract.
These changes will guarantee that at least one of MDC#getCopyOfContextMap
and ThreadContextMap#getImmutableMapOrNull
will return null
. According to the semantics of the context-propagation
project, null
means "don't propagate" and will prevent the propagation of MDC
for Log4j Core users and the propagation of ThreadContext
for Logback users.
Remark: we can also introduce a log4j.threadContext.map.bridgeAccessors
property in Log4j 3 and log4j2.threadContextMapBridgeAccessors
property in Log4j 2 to allow users that use MDC
and ThreadContext
for non-logging purposes to restore the previous behavior.