1. 程式人生 > >Java學習-051-Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError

Java學習-051-Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError

conf dep skip startup ots tin static lang 錯誤信息

錯誤信息:Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError

場景:

產品服務器采用spring-boot框架開發,zookeeper作為微服務框架,打包完成後發現服務無法啟動,tomcat啟動錯誤日誌顯示如下所示:

技術分享

日誌文本詳情:

 1 08-Nov-2017 11:32:10.984 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2 SLF4J: Class path contains multiple SLF4J bindings. 3 SLF4J: Found binding in [jar:file:/shenmajr/shenma-frss-web/webapps/ROOT/WEB-INF/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class] 4 SLF4J: Found binding in [jar:file:/shenmajr/shenma-frss-web/webapps/ROOT/WEB-INF/lib/logback-classic-1.1.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
5 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. 6 SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. 7 SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details. 8 08-Nov-2017 11:32:11.129 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
9 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]] 10 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153) 11 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) 12 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) 13 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) 14 at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940) 15 at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1816) 16 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 17 at java.util.concurrent.FutureTask.run(FutureTask.java:266) 18 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 19 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 20 at java.lang.Thread.run(Thread.java:745) 21 Caused by: java.lang.ExceptionInInitializerError 22 at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72) 23 at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:45) 24 at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150) 25 at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124) 26 at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412) 27 at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357) 28 at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155) 29 at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132) 30 at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:273) 31 at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:85) 32 at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169) 33 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5292) 34 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) 35 ... 10 more 36 Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details. 37 at org.slf4j.impl.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:54) 38 ... 23 more

解決方法如下所示:

類庫沖突是因為tomcat在啟動加載類庫時,加載類引起的。通過mvn dependency:tree命令查看maven項目工程類庫的依賴關系,可以發現如下均有引用slf4j-log4j12的類庫,因而在項目工程中將此引用依賴通過exclusions取消即可。

技術分享

(o)…,上圖中紅框錯位了,大家勿怪哈

技術分享

實際項目中,可能存在各種各樣的沖突,但只要稍用心排查一下沖突的依賴,解決此類問題還是比較容易的。

Java學習-051-Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError