【问题标题】:Spring boot logging. Multiple slf4j implementations found (logback and gradle)春季启动日志记录。找到多个 slf4j 实现(logback 和 gradle)
【发布时间】:2021-12-18 13:05:00
【问题描述】:

我正在使用 gradle+kotlin 来构建我的 Spring Boot 应用程序。在应用程序启动时,我收到以下错误。我理解为什么会出现这个错误,因为我有 2 个不同的 slf4j 实现:logback 和 org.slf4j.impl(在 gradle-api.6.9.1.jar 内)。我的 build.gradle 文件中没有包含 gradleApi() 依赖项。它是自己出现的。

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)

您是否知道为什么这个 gradle-api.6.9.1.jar 会出现在类路径中以及如何从类路径中删除这个 jar (gradle-api.6.9.1.罐子)? 有什么方法可以手动配置 slf4j 实现的选择?

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"

请分享您的任何想法。

【问题讨论】:

    标签: java spring-boot gradle slf4j gradle-kotlin-dsl


    【解决方案1】:

    你可以gradle dependencies

    并检查哪个依赖项正在使用 slf4j 并排除一个使用

    这样的排除命令 exclude(group="org.slf4j", module="slf4j-log4j12"

    我不太确定这个简单的修复,但你可以尝试先将它添加到你的 build.gradle

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

    【讨论】:

    • 感谢您的回答。已经尝试过这样做。依赖树只包含 ch.qos.logback。它没有显示任何其他 slf4j 实现。另外我不明白为什么 gradle-api.jar 出现在应用程序类路径中(错误的传递 slf4j 实现完全来自这个 jar。)
    • 欢迎您,但您可以分享 gradle.build 文件吗?如果它不是显而易见的解决方案,我将需要更多的挖掘。
    • 附加到问题。
    【解决方案2】:

    问题出在 kotlin-dsl gradle 插件中。它有自己的 slf4j 实现,并将其包含在最终构建中。据我了解,此记录器实现用于自定义插件和任务。 解决方法:我只是把kotlin-dsl插件改成了groovy。

    改动前:

    plugins {
      `kotlin-dsl`
    }
    

    修改后:

    plugins {
      groovy
    }
    

    我希望这对你和我都有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多