log4j slf4j 報錯致使堆棧異常

1. 背景

今天進行歡快的編程時,發現了咱們項目總體在整理完依賴後,爆發了由slf4j致使的堆棧溢出異常:html

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的版本不一樣致使的衝突呢?java

2. 查看依賴樹

2.1 命令行方式

當咱們須要處理依賴衝突的時候,咱們須要使用:spring

mvn dependency:tree|grep xxx

這個功能好是好,只是看起來費勁。apache

2.2 IntelliJ 神器登場

若是您有使用IDEA,能夠在maven文件中,右鍵->Diagrams->Show dependencies ...。 您將會看到像這樣的場景:編程

按住Ctrl+鼠標滾輪滑動,能夠放大圖片。此時按下Ctrl+F,搜索咱們關心的log4j,果真有多個,雖然明確的名字不一樣,可是確定有衝突就對了,咱們只會保留一份spring-boot-starter-logging中依賴的兩個版本,因此其它只要涉及到log的,都排除就對了! 故,咱們排除tablestore中的log4j:api

<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,此時再啓動,項目恢復正常。maven

3.總結

日誌打印的jar包是多種多樣的,咱們應當每種只保留一份。 另外,若是咱們依賴的jar中出現了各類各樣的jar,超出了log4jlogback,的範疇,那麼咱們應該排除全部的日誌,單獨聲明日誌打印的jar包。spring-boot

相關文章
相關標籤/搜索