log4j slf4j 报错导致堆栈异常

1. 背景

今天进行欢快的编程时,发现了我们项目整体在整理完依赖后,爆发了由slf4j导致的堆栈溢出异常:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/F:/mavenRepo/org/apache/logging/log4j/log4j-slf4j-impl/2.10.0/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/F:/mavenRepo/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.apache.logging.slf4j.Log4jLoggerFactory]

java.lang.StackOverflowError
	at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
	at org.apache.logging.log4j.spi.LoggerRegistry.getOrCreateInnerMap(LoggerRegistry.java:140)
	at org.apache.logging.log4j.spi.LoggerRegistry.hasLogger(LoggerRegistry.java:154)
	at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:38)
	at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:37)
	at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:29)
	at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:52)
	at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)

人生第一次遇到这种异常,感觉意义非凡,记录一下。刚开始,我没有任何思路,茫然地翻找资料,在看到《slf4j+log4j+logback总结》这篇文章的时候,发现了kafka中也有此类问题。我的问题是不是也是log4j的版本不同导致的冲突呢?

2. 查看依赖树

2.1 命令行方式

当我们需要处理依赖冲突的时候,我们需要使用:

mvn dependency:tree|grep xxx

这个功能好是好,只是看起来费劲。

2.2 IntelliJ 神器登场

如果您有使用IDEA,可以在maven文件中,右键->Diagrams->Show dependencies ...。 您将会看到像这样的场景:

按住Ctrl+鼠标滚轮滑动,可以放大图片。此时按下Ctrl+F,搜索我们关心的log4j,果然有多个,虽然明确的名字不同,但是肯定有冲突就对了,我们只会保留一份spring-boot-starter-logging中依赖的两个版本,所以其它只要涉及到log的,都排除就对了! 故,我们排除tablestore中的log4j:

           <dependency>
                <groupId>com.aliyun.openservices</groupId>
                <artifactId>tablestore</artifactId>
                <classifier>jar-with-dependencies</classifier>
                <version>${com.aliyun.openservices.tablestore}</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.google.protobuf</groupId>
                        <artifactId>protobuf-java</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.apache.logging.log4j</groupId>
                        <artifactId>log4j-slf4j-impl</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.apache.logging.log4j</groupId>
                        <artifactId>log4j-core</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.apache.logging.log4j</groupId>
                        <artifactId>log4j-api</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

我们再在依赖图中寻找,发现已经没有了冲突的jar,此时再启动,项目恢复正常。

3.总结

日志打印的jar包是多种多样的,我们应当每种只保留一份。 另外,如果我们依赖的jar中出现了各种各样的jar,超出了log4jlogback,的范畴,那么我们应该排除所有的日志,单独声明日志打印的jar包。

转载于:https://my.oschina.net/hengbao666/blog/3006043

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值