Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j

前端 未结 4 2381
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 10:23

In my Spring boot 2 project:

In build.gradle:

dependencies {
    implementation \'com.google.code.gson:gson:2.7\'
    imple         


        
相关标签:
4条回答
  • 2021-02-19 10:46

    I excluded spring boot logging from build.gradle but issue was still occurring. It got resolved by removing org.apache.logging.log4j/log4j-slf4j-impl/2.12.1 from .classpath

    0 讨论(0)
  • 2021-02-19 10:51

    Spring boot 2.3.0.RELEASE version, support Log4j2 natively, for logging configuration if it is on the classpath. In this case, you can simply remove other log4j dependencies.

    In other case, if you use the starters for assembling dependencies, you have to exclude Logback and then include log4j 2 instead:

    You can do like that with Gradle:

    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-log4j2'
    }
    
    configurations {
        all {
            exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        }
    }
    

    Or with Maven:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <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>
    </dependency>
    

    More information on the official documentation: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-log4j-for-logging

    0 讨论(0)
  • 2021-02-19 11:09

    According with Spring's documentation (as pointed by Simon), we wave to exclude the "spring-boot-starter-logging" module from all libraries, not only from "spring-boot-starter-web".

    configurations {
        ...
        all {
            exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        }
    }
    

    ...instead of...

    dependencies {
        ...
        implementation('org.springframework.boot:spring-boot-starter') {
            exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        }
    }
    

    Just had myself the same problem and solved with this solution.

    0 讨论(0)
  • 2021-02-19 11:11

    Even after correcting classpath, I had the same problem in STS and Gradle 'Refresh All' solved it.

    0 讨论(0)
提交回复
热议问题