Java项目解决SLF4J绑定冲突

发布时间 2023-11-15 14:49:24作者: ꧁ʚ星月天空ɞ꧂

报错

OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
SLF4J: Class path contains multiple SLF4J providers.
SLF4J: Found provider [org.slf4j.simple.SimpleServiceProvider@49e202ad]
SLF4J: Found provider [ch.qos.logback.classic.spi.LogbackServiceProvider@1c72da34]
SLF4J: See https://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual provider is of type [org.slf4j.simple.SimpleServiceProvider@49e202ad]
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.slf4j.simple.SimpleLoggerFactory loaded from file:/E:/gradle/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-simple/2.0.7/bfa4d4dad645a5b11c022ae0043bac2df6cf16b5/slf4j-simple-2.0.7.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.simple.SimpleLoggerFactory
	at org.springframework.util.Assert.instanceCheckFailed(Assert.java:713)

从上述信息可以看到slf4j日志与logback冲突了,导致slf4j报错了,不知道绑定什么了

解决SLF4J冲突和JVM警告问题

看起来你遇到了与应用程序类路径中SLF4J(Simple Logging Facade for Java)绑定冲突相关的问题。错误表明存在多个SLF4J提供程序,导致日志设置中的冲突。

关于已弃用选项-Xverify:none和-noverify的警告与JVM选项有关,可能与SLF4J问题直接无关。

主要错误消息指示两个SLF4J提供程序之间存在冲突:org.slf4j.simple.SimpleServiceProvider和ch.qos.logback.classic.spi.LogbackServiceProvider。你遇到的IllegalArgumentException是由于这些提供程序之间的冲突引起的。

为解决此问题,请按照以下步骤操作:

1. 识别依赖冲突:

检查项目的依赖关系,查看为什么slf4j-simple和logback-classic都存在于类路径中。你可能需要检查构建配置,如Gradle或Maven文件,以解决此冲突。

2. 排除或删除冲突的依赖项:

你应该在构建配置中明确排除冲突的依赖项,以确保只有一个SLF4J实现存在。例如,如果你使用Gradle,可能需要排除slf4j-simple以支持logback-classic:

对于gradle,在build.gradle中添加如下设置:

configurations {
    all {
        exclude group: 'org.slf4j', module: 'slf4j-simple'
    }
}

对于Maven,你可以在pom.xml文件中排除依赖项:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.7</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. 确保只有一个SLF4J提供程序:

在排除或删除冲突的SLF4J提供程序后,确保类路径中只有一个SLF4J绑定(最好是logback-classic),以避免冲突。

4. 重新构建和测试:

进行这些更改后,重新构建项目并测试应用程序,确保问题已解决。

请记住验证你的依赖关系和配置,以防止将来出现类似的冲突。