
SLF4J 绑定冲突的问题
问题如图
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/mvn_repo_3/mvn_repo/org/slf4j/slf4j-log4j12/1.7.30/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/mvn_repo_3/mvn_repo/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError.
SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
java.lang.ExceptionInInitializerError
at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:45)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:417)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:362)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:388)
at com.comstar.cap.tbs.util.SppiFileParserUtil.<clinit>(SppiFileParserUtil.java:18)
Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
at org.slf4j.impl.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:54)
... 8 more
Exception in thread "main"
SLF4J 检测到在类路径中存在多个绑定,这导致了 ExceptionInInitializerError。错误信息指出同时存在 log4j-over-slf4j 和 slf4j-log4j12 这两个绑定,这会导致 SLF4J 初始化失败。
如图,我们使用的是slf4j ,我们只要看一下slf4j-log4j12是谁依赖的包,然后将它在pom文件中进行排除即可
mvn dependency:tree
通过执行如上命令,可以找到zookeeper 依赖这个包
我们将下面的加入pom文件中即可
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
因为这是子工程,所以排除没有问题,如果不是的话可能带来以下问题
排除 slf4j-log4j12
依赖可能会影响 zookeeper
的日志功能,因为 slf4j-log4j12
是 SLF4J 的一个实现,负责将 SLF4J 日志记录到 Log4j。如果 zookeeper
的某些功能依赖于 SLF4J 进行日志记录,那么在排除该依赖后,可能会导致以下情况:
日志功能失效:如果
zookeeper
使用 SLF4J 进行日志记录,而没有其他的 SLF4J 实现,日志可能会停止工作。运行时错误:如果
zookeeper
代码中存在对 SLF4J 日志记录的调用,而没有有效的 SLF4J 实现,可能会导致运行时错误。
解决方案
为了避免影响 zookeeper
的使用,你可以考虑以下几种方案:
使用其他 SLF4J 实现:如果你排除了
slf4j-log4j12
,可以引入其他的 SLF4J 实现,例如slf4j-simple
或logback
,以确保日志功能正常:<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.30</version> </dependency>
保持 SLF4J 绑定:如果
zookeeper
需要slf4j-log4j12
,则可以选择不排除它,并解决其他 SLF4J 绑定冲突,例如确保只有一个 SLF4J 实现存在于类路径中。检查 Zookeeper 文档:查看
zookeeper
的文档,了解它对日志记录的具体需求,确保在排除依赖后不会影响其核心功能。
结论
在排除 slf4j-log4j12
之前,最好确认 zookeeper
是否依赖于它进行日志记录,并考虑引入其他 SLF4J 实现,以确保日志功能正常。这样可以在解决依赖冲突的同时,保持 zookeeper
的正常使用。