Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logb

这篇博客讨论了在项目中遇到的SLF4J与日志框架冲突的错误,具体表现为多个SLF4J绑定存在,如slf4j-log4j12-1.7.29.jar和logback-classic-1.2.3.jar。官方建议移除多余的绑定,并给出了在pom.xml中排除冲突依赖的示例,特别是针对Zookeeper3.7.0中内置的slf4j依赖。解决方案是通过排除Zookeeper依赖中的slf4j-log4j12来消除冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath.

在这里插入图片描述

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/mavanRepo/org/slf4j/slf4j-log4j12/1.7.29/slf4j-log4j12-1.7.29.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/mavanRepo/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: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

Caused by: 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.impl.Log4jLoggerFactory loaded from file:/opt/mavanRepo/org/slf4j/slf4j-log4j12/1.7.29/slf4j-log4j12-1.7.29.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.Log4jLoggerFactory

报错

一眼就能看到:classpath上有多个SLF4J的jar绑定,哪两个jar呢?slf4j-log4j12-1.7.29.jar和logback-classic-1.2.3.jar。

官方回答

点开上面日志中的链接,官方回答如下,我们只需要看第一句即可,即SLF4J API只能和一个日志框架绑定,如果当前classpath上有不止一个日志框架的jar,那么会提示警告并列出所有的日志框架的jar信息。所以显然是因为我们导入的jar中有多个日志框架,就是上面列出的slf4j-log4j12-1.7.30.jar和logback-classic-1.2.3.jar,从pom.xml中排除即可。

我这个错误是因为在zookeeper3.7.0里面自带了slf4j这个jar包,会和starter中的冲突,需要排除

	<!--zookeeper3.7.0里面自带了slf4j这个jar包,会和starter中的冲突,需要排除-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.7.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

在依赖
图中就可以看得见
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值