?
?重磅!盹貓的個人小站正式上線啦~誠邀各位技術大佬前來探秘!?
這里有:
- 硬核技術干貨:編程技巧、開發經驗、踩坑指南,帶你解鎖技術新姿勢!
- 趣味開發日常:代碼背后的腦洞故事、工具測評,讓技術圈不再枯燥~
- 獨家資源分享:開源項目、學習資料包,助你打怪升級快人一步!
👉?點擊直達→?盹貓貓的個人小站?👈
🌟 來逛逛吧,說不定能挖到你正在找的技術寶藏哦~
目錄
??1. 🐛 錯誤現象??
??報錯信息(關鍵部分)
??復現步驟?
??2. 🔍 問題分析??
??可能的原因?
??3. 🛠? 解決方案??
??修復代碼?
??4. 📚 經驗總結??
歡迎來到盹貓的博客
本篇文章主要介紹了
[關于"LoggerFactory is not a Logback LoggerContext but Logback is on ......"的解決方案]
?博主廣交技術好友,喜歡文章的可以關注一下?
📅 日期??:2025-07-30
??🔧 技術棧??:Spring Boot 2.x / JDK 8?
??🏷? 標簽??:#Java #BugFix #SpringBoot #Debug
??1. 🐛 錯誤現象??
??報錯信息(關鍵部分)
在引入百度短信調用SDK依賴以后
<dependency><groupId>com.baidubce</groupId><artifactId>bce-java-sdk</artifactId><version>0.10.362</version>
</dependency>
?運行時出現下述錯誤.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/seaua/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.15.0/log4j-slf4j-impl-2.15.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/seaua/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/seaua/.m2/repository/org/slf4j/slf4j-reload4j/1.7.36/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/seaua/.m2/repository/org/slf4j/slf4j-simple/1.7.36/slf4j-simple-1.7.36.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 "restartedMain" java.lang.reflect.InvocationTargetExceptionat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: 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:/home/seaua/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.15.0/log4j-slf4j-impl-2.15.0.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.Log4jLoggerFactoryat org.springframework.util.Assert.instanceCheckFailed(Assert.java:702)at org.springframework.util.Assert.isInstanceOf(Assert.java:621)at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:294)at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:118)at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:238)at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:220)at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:79)at org.springframework.boot.SpringApplicationRunListeners.lambda$starting$0(SpringApplicationRunListeners.java:56)at java.util.ArrayList.forEach(ArrayList.java:1259)at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:56)at org.springframework.boot.SpringApplication.run(SpringApplication.java:298)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)at com.uav.BootApplication.main(BootApplication.java:14)... 5 more進程已結束,退出代碼0
??復現步驟?
使用排除法,引入該依賴或注釋掉該pom依賴進行程序運行來判斷是否是該依賴引起的問題.
??2. 🔍 問題分析??
??可能的原因?
?出現問題后首先看一下主要的報錯翻譯,內容如下:
java.lang.IllegalArgumentException:LoggerFactory 不是 Logback LoggerContext,但類路徑中包含 Logback。請移除 Logback 或其競爭實現(從文件:/home/seaua/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.15.0/log4j-slf4j-impl-2.15.0.jar 加載的類 org.apache.logging.slf4j.Log4jLoggerFactory)。如果您使用的是 WebLogic,則需要在 WEB-INF/weblogic.xml 的 prefer-application-packages 中添加“org.slf4j”:org.apache.logging.slf4j.Log4jLoggerFactory
這里提到了?移除Logback和slf4j,則可能是Logback與其它日志依賴代碼沖突,導致啟動時不知道使用哪個類.
??3. 🛠? 解決方案??
??修復代碼?
?對存在問題的依賴進行移除,保證代碼正常運行.
??修復前依賴(問題代碼)
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>bce-java-sdk</artifactId>
<version>0.10.362</version>
</dependency>
???修復后依賴(安全代碼)?
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>bce-java-sdk</artifactId>
<version>0.10.362</version>
<exclusions><exclusion><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId></exclusion><exclusion><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId></exclusion>
</exclusions>
</dependency>
??4. 📚 經驗總結??
????????該錯誤主要容易出現在引入過多三方依賴的情況下,由于不同公司使用的日志依賴版本不同,而導致的版本沖突問題,在排除沖突依賴后,程序可以正常啟動還有就是一定要從報錯中發現問題,直接翻譯一下就基本可以定位問題所在.???
??📢 你的代碼里有沒有類似的坑?歡迎評論區討論!?? 🚀
??🔗 相關資源?
?如果你對區塊鏈內容感興趣可以查看我的專欄:小試牛刀-區塊鏈?
???? 作者??:盹貓
??📅 最后更新??:2025-07-30
??🔔 關注我,獲取更多技術干貨!?? 🚀