開發常見錯誤之 : Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar


SLF4J: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError.
SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
Exception in thread "main" Java.lang.ExceptionInInitializerError
at org.apache.log4j.LogManager.getLogger(LogManager.java:44)
at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:66)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:277)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:288)
....
Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
at org.apache.log4j.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:51)
... 11 more

 

原因是引用了兩個關於log4j的jar,造成死循環

log4j-over-slf4j-1.7.7.jar

slf4j-log4j12-1.7.7.jar

但只引用slf4j-log4j12-1.7.7.jar會提示org.apache.log4j.Level這個class找不到

要使用log4j記錄日志,其實只用引用log4j-over-slf4j-1.7.7.jar就可以了,一般在maven中引入其他依賴時,可以排除對log4j的引用:

<exclusion>
    <groupid>log4j</groupid>
    <artifactId>log4j</artifactId>
</exclusion>
<exclusion>
    <groupid>org.slf4j</groupid>
    <artifactId>elf4j-log4j12</artifactId>
</exclusion>

 


免責聲明!

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



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