MDC issues with SLF4J

6k views Asked by At

We use SLF4J (with log4j) as our logging framework. We are trying to leverage the MDC feature which as per online documentation is supported by Log4j.

MDC does not work when SLF4J is used. However, when log4j is used instead, it works perfectly fine. The documentation states that as long as the underlying framework supports MDC, SLF4J should support it.

We are using SLF4J 1.6.4 (slf4j-api, slf4j-log4j12 have been added as dependencies in our pom.xml).

Is there a discrepancy/mismatch somewhere? Are we missing any dependencies? Any inputs would be appreciated.

2

There are 2 answers

0
Matruskan On

According to SLF4J's MDC javadoc, the class uses NOPMDCAdapter, an empty implementation, for slf4j-simple :

https://www.slf4j.org/api/org/slf4j/MDC.html

If the underlying logging system offers MDC functionality, then SLF4J's MDC, i.e. this class, will delegate to the underlying system's MDC. Note that at this time, only two logging systems, namely log4j and logback, offer MDC functionality. For java.util.logging which does not support MDC, BasicMDCAdapter will be used. For other systems, i.e. slf4j-simple and slf4j-nop, NOPMDCAdapter will be used.

For NOPMDCAdapter documentation: https://www.slf4j.org/api/org/slf4j/helpers/NOPMDCAdapter.html

0
Ceki On

SLF4J definitely supports MDC and should work out of the box. Which version of slf4j-api.jar and slf4j-logj12.jar are you using?

In an earlier version of the question you had placed slf4j-simple.jar on the class path which might be the cause of the problem.