简体   繁体   中英

Spring boot logging. Multiple slf4j implementations found (logback and gradle)

I'am using gradle+kotlin to build my spring boot application. In time of application starting I've receiving the following error. I understand why this error appeared, because I have 2 different slf4j implementations: logback and org.slf4j.impl(inside gradle-api.6.9.1.jar ). I don't include gradleApi() dependency to my build.gradle file. It's appears by it self.

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/user/.gradle/caches/6.9.1/generated-gradle-jars/gradle-api-6.9.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/user.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.6/b09efa852337fa0dd9859614389eec58dc287116/logback-classic-1.2.6.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 [org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext]
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext loaded from file:/C:/Users/user/.gradle/caches/6.9.1/generated-gradle-jars/gradle-api-6.9.1.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext
  at org.springframework.util.Assert.instanceCheckFailed(Assert.java:702)
  at org.springframework.util.Assert.isInstanceOf(Assert.java:621)
  at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:294)
  at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:118)
  at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:232)
  at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:213)
  at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
  at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
  at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
  at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
  at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:76)
  at org.springframework.boot.SpringApplicationRunListeners.lambda$starting$0(SpringApplicationRunListeners.java:53)
  at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
  at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
  at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:53)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:329)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332)
  at com.altruist.accounts.Application.main(Application.java:12)

Do you have any ideas why this gradle-api.6.9.1.jar appears in classpath and How can I remove this jar from classpath ( gradle-api.6.9.1.jar )? Does any way to configure chooses of slf4j implementations manually?

build.gradle.kts

val lombokVersion: String by project
val mapstructVersion: String by project

plugins {
    id("org.springframework.boot")
}

tasks {
    bootJar {
        archiveFileName.set(project.name + ".jar")
        mainClass.set("com.package.Application")
    }
}

publishing {
    publications {
        create<MavenPublication>("bootJava") {
            artifact(tasks.getByName("bootJar"))
        }
    }
}

springBoot  {
    buildInfo()
}


dependencies {
    implementation(project(":api-layer"))
    implementation("com.package:integration-app-api:1.0.0-SNAPSHOT")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa:2.5.5")
    implementation("org.mapstruct:mapstruct:1.4.2.Final")
    implementation("com.h2database:h2:1.4.200")
    implementation("ch.qos.logback:logback-classic:1.2.6")
    implementation("org.springframework.boot:spring-boot-starter-actuator:2.5.5")
    implementation("org.springframework.cloud:spring-cloud-starter-openfeign:3.0.5")

    compileOnly("org.mapstruct:mapstruct-processor:$mapstructVersion")
    annotationProcessor("org.mapstruct:mapstruct-processor:$mapstructVersion")
    testImplementation("org.springframework.boot:spring-boot-starter-test:2.5.5")
}

description = "api-layer-service"

Please share your any ideas.

You can do gradle dependencies

and check which dependency is using slf4j and exclude one using

the exclude command like this exclude(group="org.slf4j", module="slf4j-log4j12"

Am not pretty sure about this easy fix but you can try to add this to your build.gradle first

configurations.all {
   exclude(group="org.slf4j", module="slf4j-log4j12"
}

Problem was in kotlin-dsl plugin for gradle. It has it's own slf4j implementation and includes it in final build. As I understand this logger implementation used for custom plugins and tasks. Solution: I just changed kotlin-dsl plugin to groovy.

Before changes:

plugins {
  `kotlin-dsl`
}

After changes:

plugins {
  groovy
}

I hope this helps you as well as me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM