Spring 引导日志记录。找到多个 slf4j 实现(logback 和 gradle)
Spring boot logging. Multiple slf4j implementations found (logback and gradle)
我正在使用 gradle+kotlin 构建我的 spring 启动应用程序。在申请开始时,我收到以下错误。我明白为什么会出现这个错误,因为我有 2 个不同的 slf4j 实现:logback 和 org.slf4j.impl(inside gradle-api.6.9.1.jar )。我没有将 gradleApi() 依赖项包含到我的 build.gradle 文件中。是自己出现的
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[=12=](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.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(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
}
我希望这对你和我都有帮助。
我正在使用 gradle+kotlin 构建我的 spring 启动应用程序。在申请开始时,我收到以下错误。我明白为什么会出现这个错误,因为我有 2 个不同的 slf4j 实现:logback 和 org.slf4j.impl(inside gradle-api.6.9.1.jar )。我没有将 gradleApi() 依赖项包含到我的 build.gradle 文件中。是自己出现的
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[=12=](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.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(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
}
我希望这对你和我都有帮助。