考虑到log4j很久不更新、性能相对弱,以及一些项目本身的原因,经过较为谨慎的考虑,决定改用logback。迁移还是比较顺利的,花了1个小时左右就搞定了,做个简单的笔记。
(1) 首先去掉所有log4j相关的依赖,主要有:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
以及:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
(2) 为项目添加以下依赖:
<!-- logback日志配置开始 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>
<!-- logback日志配置结束 -->
(3) 删除项目/src/main/resources目录下的log4j.properties,并在该目录添加logback.xml文件,logback.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target>
<encoder>
<pattern>%date [%level] [%thread] %logger{80} [%file : %line] %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%date [%level] [%thread] %logger{80} [%file : %line] %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.home}/logs/yzf/sql%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 保留 30天数据,默认无限-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
</appender>
<logger name="org.springframework" level="INFO" />
<logger name="org.apache" level="INFO" />
<logger name="com.itmuch" level="WARN" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
TIPS
(1) log4j-over-slf4j与slf4j-log4j12不能共存,可能会引发异常,详见http://www.tuicool.com/articles/INveIf 。
(2) slf4j-api、slf4j-log4j12以及log4j之间的关系http://blog.csdn.net/tengdazhang770960436/article/details/18006127
(3) 参考文档:http://blog.csdn.net/chenminghe271/article/details/38682493
本文首发
http://www.itmuch.com/work-sum-log4j-to-logback/
网友评论