1

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?

2 Answers2

0

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>
0

if your class path contain multiple SLF4J bindings. then you can exclude others and keep only one binding. in springboot , other dependant jars could add slf4j or other logging frameworks to your project. run below maven command to find which jar is bringing these bindings and then exclude them from pom

mvn dependency:tree -Dincludes=:slf4j*

to exclude a jar from another dependancy add this to the dependancy

<exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
        </exclusion>
    </exclusions>
arvin_v_s
  • 1,036
  • 1
  • 12
  • 18