首先搞清楚SLF4J和log4j是啥子关系,
然后 https://logging.apache.org/log4j/2.x/log4j-slf4j-impl/
The Log4j 2 SLF4J Binding allows applications coded to the SLF4J API to use Log4j 2 as the implementation.
Due to a break in compatibility in the SLF4J binding, as of release 2.11.1 two SLF4J to Log4j Adapters are provided.
1. log4j-slf4j-impl should be used with SLF4J 1.7.x releases or older.
2. log4j-slf4j18-impl should be used with SLF4J 1.8.x releases or newer.
Applications that take advantage of the Java Module System should use SLF4J 1.8.x and log4j-slf4j18-impl
log4j-slf4j-impl should be used with SLF4J 1.7.x releases or older.
log4j-slf4j18-impl should be used with SLF4J 1.8.x releases or newer.
所以排查下项目正真使用的SLF4J版本是不是1.8以上的,如果是,请使用log4j-slf4j18-impl
,一般是引入新依赖覆盖导致这个问题。
遇到此类问题,要灵活,比如这回是 1.7.x
,下回就可能是1.6.x
引起的不兼容。出现问题时多怀疑自己的代码,即使在一个相对复杂的系统中,也要仔细阅读堆栈,不要看见就头大,因为它先前肯定是运行正常的,是否正确按照说明正确使用了,每个函数的调用是否明白其含义,最近的变动是啥。
附录
log4j2 + slf4j 配置
https://howtodoinjava.com/log4j2/log4j2-with-slf4j/
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.20.0</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %l - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
<logger name="org.apache.http" level="debug">
<AppenderRef ref="Console"/>
</logger>
</Loggers>
</Configuration>