I have a Spring boot project with the following dependency:
[INFO] +- org.springframework.boot:spring-boot-starter-log4j:jar:1.3.8.RELEASE:compile
[INFO] | +- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile
[INFO] | +- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] | +- org.slf4j:slf4j-log4j12:jar:1.7.25:compile
[INFO] | \- log4j:log4j:jar:1.2.17:compile
And MDC usage:
import org.slf4j.MDC;
MDC.put("sheker", "kazav");
MDC.put("correlationId", "nonsense");
org.apache.log4j.Logger.getLogger(DEFAULT).info("the logger string");
In the log4j.xml I`m trying to print the Mapped Diagnostic Context keys as following:
<appender name="console" class="com.sheker.CustomConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%X{correlationId}] [%X{sheker}] - %m" />
</layout>
</appender>
I'm expecting to get:
[nonsense] [kazav] - the logger string
But the actual output is:
[] [] - the logger string
Why are the MDC keys get ignored?
the problem was:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/path/repository/ch/qos/logback/logback-classic/1.1.11/logback-classic-1.1.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/path/repository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
the solution is:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>