LoggerFactory is not a Logback LoggerContext but Logback is on the classpath

215.2k views Asked by At

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")
12

There are 12 answers

2
newbie On BEST ANSWER

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"
}
1
michals On

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

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
0
Anand Rockzz On

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.

0
Gabriel Sterz On

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

0
Adán Escobar On

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

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.

1
ufukomer On

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

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.

1
Praj On

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'
}
2
razvang On
            <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.

3
Robin Wang On

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