[英]Spring boot logging. Multiple slf4j implementations found (logback and gradle)
I'am using gradle+kotlin to build my spring boot application.我正在使用 gradle+kotlin 来构建我的 Spring Boot 应用程序。 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 ).
我明白为什么会出现这个错误,因为我有 2 个不同的 slf4j 实现:logback 和 org.slf4j.impl(inside gradle-api.6.9.1.jar )。 I don't include gradleApi() dependency to my build.gradle file.
我的 build.gradle 文件中没有包含 gradleApi() 依赖项。 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 )?你有什么想法为什么这个gradle-api.6.9.1.jar出现在类路径中,以及如何从类路径( gradle-api.6.9.1.jar )中删除这个 jar ? Does any way to configure chooses of slf4j implementations manually?
有没有办法手动配置 slf4j 实现的选择?
build.gradle.kts 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
你可以做
gradle dependencies
and check which dependency is using slf4j and exclude one using并检查哪个依赖项正在使用 slf4j 并使用排除一个
the exclude command like this exclude(group="org.slf4j", module="slf4j-log4j12"
像这样的 exclude 命令
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我不太确定这个简单的修复,但你可以先尝试将它添加到你的 build.gradle
configurations.all {
exclude(group="org.slf4j", module="slf4j-log4j12"
}
Problem was in kotlin-dsl plugin for gradle.问题出在 gradle 的 kotlin-dsl 插件中。 It has it's own slf4j implementation and includes it in final build.
它有自己的 slf4j 实现并将其包含在最终版本中。 As I understand this logger implementation used for custom plugins and tasks.
据我了解,这个记录器实现用于自定义插件和任务。 Solution: I just changed kotlin-dsl plugin to groovy.
解决方案:我刚刚将 kotlin-dsl 插件更改为 groovy。
Before changes:改动前:
plugins {
`kotlin-dsl`
}
After changes:更改后:
plugins {
groovy
}
I hope this helps you as well as me.我希望这对你和我都有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.