[英]Spring boot logging. Multiple slf4j implementations found (logback and gradle)
我正在使用 gradle+kotlin 來構建我的 Spring Boot 應用程序。 在應用程序開始時,我收到以下錯誤。 我明白為什么會出現這個錯誤,因為我有 2 個不同的 slf4j 實現:logback 和 org.slf4j.impl(inside 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出現在類路徑中,以及如何從類路徑( gradle-api.6.9.1.jar )中刪除這個 jar ? 有沒有辦法手動配置 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"
請分享您的任何想法。
你可以做gradle dependencies
並檢查哪個依賴項正在使用 slf4j 並使用排除一個
像這樣的 exclude 命令exclude(group="org.slf4j", module="slf4j-log4j12"
我不太確定這個簡單的修復,但你可以先嘗試將它添加到你的 build.gradle
configurations.all {
exclude(group="org.slf4j", module="slf4j-log4j12"
}
問題出在 gradle 的 kotlin-dsl 插件中。 它有自己的 slf4j 實現並將其包含在最終版本中。 據我了解,這個記錄器實現用於自定義插件和任務。 解決方案:我剛剛將 kotlin-dsl 插件更改為 groovy。
改動前:
plugins {
`kotlin-dsl`
}
更改后:
plugins {
groovy
}
我希望這對你和我都有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.