springboot log4j2 報(bào)錯(cuò)SLF4J: Class path contains multiple SLF4J bindings砚哗,maven依賴(lài)重復(fù)沖突的解決辦法
springboot 使用log4j保存日志慰照,網(wǎng)上隨便找了篇博文參考允悦,配置依賴(lài)照寫(xiě),啟動(dòng)結(jié)果報(bào)錯(cuò): Class path contains multiple SLF4J bindings
具體報(bào)錯(cuò)如下:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/maven_repository_new/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/maven_repository_new/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]
Exception in thread "main" java.lang.StackOverflowError
at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:108)
at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:121)
at org.apache.logging.log4j.util.StackLocatorUtil.getCallerClass(StackLocatorUtil.java:55)
at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:42)
at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
報(bào)錯(cuò)字面意思 有多個(gè) SLF4J 綁定,log4j-slf4j-impl-2.11.2.jar 和 logback-classic-1.2.3.jar里面重復(fù)綁定SLF4J 擒抛,再看了下那博文,有這么一段绽乔,如項(xiàng)目中有導(dǎo)入spring-boot-starter-web依賴(lài)包記得去掉spring自帶的日志依賴(lài)spring-boot-starter-logging弧蝇,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
按上面說(shuō)法是 spring-boot-starter-web 自帶日志依賴(lài),那既然exclusion排除了日志依賴(lài)咋還有問(wèn)題折砸!
實(shí)際是 spring-boot-starter 自帶日志依賴(lài) 而不是 spring-boot-starter-web看疗,下面才是正確的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
如何快速正確的解決類(lèi)似重復(fù)依賴(lài)的問(wèn)題。
1 右鍵pom.xml -> Maven -> Show Dependencies
2 搜索重復(fù)的jar包被哪些上層依賴(lài)
比如 這里要搜索的是 logback-classic-1.2.3.jar 在哪里被依賴(lài)
在上面打開(kāi)的依賴(lài)關(guān)系圖中 crtl + f 打開(kāi)搜索睦授,然后輸入搜索內(nèi)容 logback-classic两芳,如下:
點(diǎn)擊找到的logback-classic ,會(huì)看到如下依賴(lài)關(guān)系:
3 找到了需要排除的jar被誰(shuí)用到
那就在pom.xml中用排除掉去枷,
如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
最后
其他類(lèi)似的maven 重復(fù)依賴(lài)問(wèn)題怖辆,可以按上面方法嘗試去解決!