Logging with log4j2 in Spring Boot app

5.1k views Asked by At

I am trying to learn Spring Boot. But I completely mess up with a loggers dependencies. I have a simple pom:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Logging -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.3</version>
        </dependency>
    </dependencies>

I've created log4j2.xml in ..src\main\resources\

I logging with something like this:

private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);
LOG.error("ERRRRRRR!!!!");

But when app starts I see:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/me/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/me/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.3/log4j-slf4j-impl-2.3.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]

And of course log4j2 and its config don't work.

I know, it is qute popular question. I googled a lot of answers, but nothing works for me.

2

There are 2 answers

4
Toilal On BEST ANSWER

You have multiple SLF4J bindings in your classpath.

  • Logback classic, which is included by default in spring boot
  • Log4j that you want to use.

You need to have a single one in the classpath, by using maven dependency exclusion.

0
crea1 On

You have to exclude the logback that comes bundled with spring-boot-starter-batch, and replace it with spring-boot-starter-log4j2. Check out the documentation for logging and Spring Boot.

This pom configuration works well for me.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
        <version>${spring.boot.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>