【idea】log4j和slf4j配合使用问题(2024-9-23最新版本)!
1、slf4j
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>${slf4j.version}</version> <!-- <scope>test</scope>--> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency>
2、log4j
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j2-impl</artifactId> <version>${log4j.version}</version> <!-- <scope>test</scope>--> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j.version}</version> </dependency>
3、以前只需要配置上述依赖就行
Logger logger = LoggerFactory.getLogger(CommonServiceImpl.class);
4、使用最新版本后报错
<slf4j.version>2.0.16</slf4j.version> <log4j.version>2.24.0</log4j.version>
ERROR Unable to create Lookup for ctx java.lang.NoSuchMethodError: 'java.util.stream.Stream org.apache.logging.log4j.util.ServiceLoaderUtil.safeStream(java.lang.Class, java.util.ServiceLoader, org.apache.logging.log4j.Logger)'
看报错基本都是启动服务时,内部调用报的错,去maven上看了下依赖,明确表明都是依赖log4j-api,把依赖加上就行
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${log4j.version}</version> </dependency>
log4j-slf4j2-impl的:
log4j-core的:
注:
log4j-slf4j2-impl和log4j-slf4j-impl无明显区别,只是支持log4j的日志框架不用,2是2.x版本,无后缀是1.x版本。(查询自百度AI)