Spring已经升级到5.X版本,最近在用Spring5.X集成Log4j2+Slf4j日志时,遇到了版本升级问题的困扰。按照Spring4.X的方式添加jar包,从网上查到的Maven依赖如下,
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.21version>
dependency>
添加完之后,编写log4j2.xml,然后使用以下代码进行测试:
package test;
import org.slf4j.LoggerFactory;
import java.util.logging.Logger;
/***
*@ClassName LogTest
*@Description TODO: Slf4j与Log4j2配合使用,打印日志信息
*@Author zjh
*@Data 2019/11/20 16:15
*@Version 1.0.0
***/
public class LogTest {
private static final Logger log = LoggerFactory.getLogger(LogTest.class);
public static void main(String[] args) {
log.info("hello world");
log.warning("hello world");
}
}
其中,导入的两个包是编辑器(IDEA)自动导入的。
运行后报如下错误:
java.lang.ExceptionInInitializerError Caused by: java.lang.ClassCastException: org.slf4j.impl.Log4jLoggerAdapter cannot be cast to java.util.logging.Logger at test.LogTest.
错误出现在private static final Logger log = LoggerFactory.getLogger(LogTest.class);
这一行。原因是“=”右边的类型是org.slf4j.Logger
,而左边的类型是java.util.logging.Logger
,两边的类型对不上,于是发生了错误。
-----------------------------------------------------------------分割线------------------------------------------------------------------
查了资料发现,Spring5版本废弃了很多之前4.0版本的配置类,所以要更新Maven依赖项,导入正确的日志包,才能进行愉快地玩耍。详细的配置和步骤记录如下:
需要以下4个依赖项
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.8.0-alpha2version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-slf4j-implartifactId>
<version>2.10.0version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.10.0version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.10.0version>
dependency>
我使用的Spring版本是5.0.8,如下
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.8.RELEASEversion>
dependency>
在src的resources目录下新建log4j2.xml,内容如下:
<configuration status="WARN">
<appenders>
<console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
console>
appenders>
<loggers>
<root level="info">
<appender-ref ref="Console"/>
root>
loggers>
configuration>
需要注意的是,导入正确的日志包。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
完整的测试代码如下:
package test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/***
*@ClassName LogTest
*@Description TODO: Slf4j与Log4j2配合使用,打印日志信息
*@Author zjh
*@Data 2019/11/20 16:45
*@Version 1.0.1
***/
public class LogTest {
private static final Logger log = LoggerFactory.getLogger(LogTest.class);
public static void main(String[] args) {
log.info("hello world");
log.warn("hello world");
}
}
运行,控制台输出
2019-11-20 16:47:15.743 [main] INFO test.LogTest - hello world
2019-11-20 16:47:15.754 [main] WARN test.LogTest - hello world
成功!