46

I think some module in spring-boot-starter-security is conflict with log4j, but I don't know which one.

my gradle dependence is as following:

compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-security"){
    exclude module: "spring-boot-starter-logging"
}

compile "org.apache.logging.log4j:log4j-api"
compile "org.apache.logging.log4j:log4j-core"
compile "org.apache.logging.log4j:log4j-slf4j-impl"
compile('org.apache.poi:poi:3.10.1')
compile('org.apache.poi:poi-ooxml:3.10.1')
testCompile("junit:junit")
newbie
  • 967
  • 1
  • 8
  • 17

7 Answers7

45

Same solution for maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>1.5.1.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
</dependency>
ufukomer
  • 863
  • 12
  • 15
31

i figured out

compile("org.springframework.boot:spring-boot-starter-security"){
    exclude module: "spring-boot-starter-logging"
    exclude module: "logback-classic"
}
compile("org.springframework.boot:spring-boot-starter-thymeleaf"){
    exclude module: "logback-classic"
}
newbie
  • 967
  • 1
  • 8
  • 17
  • 1
    also applies to webflux `compile(group: 'org.springframework.boot', name: 'spring-boot-starter-webflux', version: '2.2.6.RELEASE') { // exclude modules }` – prayagupa Apr 12 '20 at 21:30
  • ... and also for `spring-boot-starter-mail`, `spring-boot-starter-mongodb`, `spring-boot-starter-data-jpa`, . Phew! – potame May 30 '22 at 15:31
24

The problem for me went on with: Either remove Logback or the competing implementation class org.apache.logging.slf4j.Log4jLoggerFactory loaded from log4j-slf4j-impl-2.12.0.jar

I added

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        exclude group: 'ch.qos.logback', module: 'logback-classic'
        exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
    }
}

to my gradle.build and also refreshed all project dependencies with the latest versions and the problem resolved.

rupweb
  • 2,629
  • 1
  • 23
  • 41
10

Using the IDEA "Show Dependencies" or mvn dependency:tree -Dverbose to find all the logback-classic jar file, and exclude them.

Robin Wang
  • 731
  • 1
  • 7
  • 14
3

For me this problem appeared only during execution of maven-surefire-plugin. Somehow Logback is there on the class path, even if its not added to project dependencies. I added such fix:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>${maven-surefire-plugin.version}</version>
     <configuration>
        <systemPropertyVariables>
            <org.springframework.boot.logging.LoggingSystem>
                org.springframework.boot.logging.log4j2.Log4J2LoggingSystem
            </org.springframework.boot.logging.LoggingSystem>
        </systemPropertyVariables>
    </configuration>
</plugin>

You can also use

mvn clean install -Dorg.springframework.boot.logging.LoggingSystem=org.springframework.boot.logging.log4j2.Log4J2LoggingSystem
michals
  • 94
  • 6
  • Will do my bit: in my case it was because of a "java-gradle-plugin" in gradle plugins section – Defake Mar 20 '20 at 08:01
1

According to this documentation, Spring Boot automatically configures the Log Back library because of the web starter. You should exclude the spring-boot-starter-logging to fix the issue.

To exclude the default LogBack configuration on the gradle project,

configurations {
    all*.exclude module : 'spring-boot-starter-logging'
    all*.exclude module : 'logback-classic'
}
Praj
  • 161
  • 1
  • 3
  • Thank your for your answer! But you should consider [editting](https://stackoverflow.com/posts/69718362/edit) it to include a quote of the reference you included. This will make your answer more robust in case the reference you link to disappears. – Patrick Oct 26 '21 at 10:40
0

According to this bug, the bundled and undeletable Kotlin plugin (reminds me the forced inclusion of U2's album with iOS) includes org.gradle.kotlin.kotlin-dsl, which in turn pollutes the classpath with an incompatible logging library (even if disabled).

I solved this by changing the runner from "IntelliJ IDEA" to "Gradle" in preferences:

  • Build, Execution, Deployment
    • Build Tools
      • Gradle
        • Build an run using -> Gradle
        • Run tests using -> Gradle
Bohemian
  • 389,931
  • 88
  • 552
  • 692