60

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
  • 1,157
  • 2
  • 9
  • 17
  • This article helped me to better understand and solve the problem: https://tedblob.com/loggerfactory-is-not-a-logback-loggercontext-but-logback-is-on-the-classpath/ – Peter Aug 09 '22 at 11:28

12 Answers12

56

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
  • 1,021
  • 1
  • 14
  • 16
35

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
  • 1,157
  • 2
  • 9
  • 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
33

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
  • 3,052
  • 1
  • 30
  • 57
13

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

Robin Wang
  • 779
  • 1
  • 8
  • 16
5

Started seeing this error after including 'it.ozimov:embedded-redis:0.7.3' I had to change

testImplementation 'it.ozimov:embedded-redis:0.7.3'

to

testImplementation ('it.ozimov:embedded-redis:0.7.3') {
    exclude module: 'commons-logging'
    exclude module: 'slf4j-simple'
}

and the issue is resolved.

Anand Rockzz
  • 6,072
  • 5
  • 64
  • 71
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
  • 1
    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
3
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-simple</artifactId>
                </exclusion>
            </exclusions>

For me this was the fix, because OpenAPI Generator was bringing this and it conflicted with SpringsBoot Default. So once I excluded this one, the SpringBootApp started working.

razvang
  • 1,055
  • 11
  • 15
2

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
  • 451
  • 2
  • 5
  • 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
  • 412,405
  • 93
  • 575
  • 722
0

works for me:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <!-- avoid: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. on run with vscode -->
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
Adán Escobar
  • 1,729
  • 9
  • 15
0

Per @Bohemian 's post:

Removing this fixed it for me:

plugins {
    `kotlin-dsl`
}

Bug reference from @Bohemian

This plugin is recommended to be added to gradle files for the kotlin dsl, but I found that it really isn't needed... syntax highlighting still worked for me afterwards and I didn't see any issues after removing it. In fact it removed the issue mentioned in this thread to remove it.

TheJeff
  • 3,665
  • 34
  • 52
-1

It should be told that after trying to exclude every logback artifact I found which didn't work for me, I just deleted the maven-folder (/.m2). After another

mvn clean install

everything started fine and the error never appeared again. I by the way removed every Exclusion i did before. Still working