SpringBoot整合log4j2導入新的依賴出現jar沖突解決


1、問題復現:

之前在SpringBoot中配置整合了log4j2,今天在pom文件中,導入新的依賴(依賴如下)之后,

<dependency>
   <groupId>com.github.tobato</groupId>
   <artifactId>fastdfs-client</artifactId>
   <version>1.26.7</version>
</dependency>   

重啟SpringBoot項目時,出現如下錯誤:

Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/D:/00_Develop_Install/Maven/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.12.1/log4j-slf4j-impl-2.12.1.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.apache.logging.slf4j.Log4jLoggerFactory
    at org.springframework.util.Assert.instanceCheckFailed(Assert.java:696)
    at org.springframework.util.Assert.isInstanceOf(Assert.java:596)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:281)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:104)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:239)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:220)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
    at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:70)
    at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:47)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:305)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at com.pactera.attd.AttdApplication.main(AttdApplication.java:12)

在網上查找相關資料,大部分都是說要 排除SpringBoot默認的logback依賴,但是這個問題,我在整合時,已經去除掉了,所以,很顯然不是這個問題。后來,看來篇文章說是,pom文件依賴中的順序問題,后來就調整了一下順序,然后重啟,報了以下錯誤(報錯信息太多,這里只截取了主要部分)。

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/00_Develop_Install/Maven/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/00_Develop_Install/Maven/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.12.1/log4j-slf4j-impl-2.12.1.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 [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
Logging system failed to initialize using configuration from 'classpath:log4j2.xml'
...

2、問題解決

仔細查看了這次報錯的信息,大致意思是說,日志系統出現了多jar包沖突,由於我使用的是log4j2,結果顯示還有一個logback的jar包,於是順着這個問題,查看了一下項目所引jar包,發現了多了以下紅框中所展示的jar包,將所引入坐標注釋掉,此jar包則沒有了,如下圖所示。

引入坐標后:

注釋坐標后:

 

這下,很明顯就知道了原因,也就是說,自己新導入的pom坐標,並未將此jar包排除掉,導致出現了多jar包,既然這樣,看來跟新引入的pom坐標的順序也沒關系了,只需要將原先的logback-classic排除掉即可,於是,就在引入的pom坐標下添加了如下排除的依賴。

<exclusions>
     <exclusion>
           <groupId>ch.qos.logback</groupId>
           <artifactId>logback-classic</artifactId>
     </exclusion>
</exclusions>

前后依賴對比,如圖。

解決前:

 解決后:

注意:groupId和artifactId一定要寫正確,並對應的依賴里面。

然后,重新build項目,發現沖突的jar包,沒有了,之后重啟SpringBootApplication,項目正常啟動,問題解決。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM