这里笔者将要重现Log日志死循环,使用log4j和slf4j来说明。
报错的信息如下
在pom.xml中引入如下依赖
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.16</version>
</dependency>
最终引入的jar包有
log4j的配置文件内容
log4j.rootLogger=DEBUG, console
# console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern= %d - %-5p %c %x - %m%n
写一个日志的测试类
package org.hl.logapitest;
import org.apache.log4j.Logger;
public class LogTest {
private static final Logger logger = Logger.getLogger(LogTest.class);
public static void main(String[] args) {
logger.warn("故意使用而引起死循环");
}
}
SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError.
当同时引用了这两个依赖的时候,会出现这样的错误。
究其本质的原因是: log4j将日志扔给slf4j --> slf4j又将日志扔回去给log4j,所以循环于此,所以会报错