今天进行欢快的编程时,发现了咱们项目总体在整理完依赖后,爆发了由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
当咱们须要处理依赖冲突的时候,咱们须要使用:spring
mvn dependency:tree|grep xxx
这个功能好是好,只是看起来费劲。apache
若是您有使用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
日志打印的jar包是多种多样的,咱们应当每种只保留一份。 另外,若是咱们依赖的jar中出现了各类各样的jar,超出了log4j
、logback
,的范畴,那么咱们应该排除全部的日志,单独声明日志打印的jar包。spring-boot