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 and slf4j-simple 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.

Eyal
  • 3,412
  • 1
  • 44
  • 60
gkari
  • 229
  • 1
  • 7
  • 11
  • 5
    You shouldn't be importing both `slf4j-log4j12` and `slf4j-simple`, exactly one binding is required. This might be a problem. – Tomasz Nurkiewicz Jan 19 '12 at 20:48
  • @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. – gkari Jan 19 '12 at 21:44

2 Answers2

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

Matruskan
  • 325
  • 3
  • 11
-1

The fact that you have added slf4j-simple.jar in addition to slf4j-log4j12.jar as a dependency indicates that you are yet unfamiliar with SLF4J. Are you sure you sure SLF4J is actually bound to log4j? Leaving MDC aside, have you verified that your log4j.properties configuration file is taken into account?

Ceki
  • 26,753
  • 7
  • 62
  • 71
  • Thanks for replying. Yes, the log4jproperties configuration file is taken into account and normal logging works perfectly fine. – gkari Jan 20 '12 at 15:03