MDC issues with SLF4J
Asked Answered
C

2

6

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.

Carioca answered 19/1, 2012 at 20:45 Comment(2)
You shouldn't be importing both slf4j-log4j12 and slf4j-simple, exactly one binding is required. This might be a problem.Dott
@Tomasz, Thanks for replying. I deleted the dependency on slf4j-simple. However, that has not fixed the issue. Normal logging works but the MDC feature does not.Carioca
M
0

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

Mitrailleuse answered 3/2, 2022 at 17:36 Comment(0)
A
0

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.

Arrangement answered 26/9, 2023 at 14:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.