目的:将项目中的log4j改为logback,并且使用门面日志slf4j进行打印日志。
1.删除log4j的依赖
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j_version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j_version}</version>
</dependency>
2.添加logback和slf4j的依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j_version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback_version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback_version}</version>
</dependency>
3.添加logback.xml
在src/main/java/resources下新建logback.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" debug="false">
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
[%level] [%thread] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%mdc{traceid}] [%mdc{user_id}] [%logger{0}] [%msg]%n
</pattern>
</encoder>
</appender>
<appender name="root" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log4j.path}/root.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<maxHistory>3000</maxHistory>
<!-- 按时间回滚的同时,按文件大小来回滚 -->
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1GB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>
[%level] [%thread] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%mdc{traceid}] [%mdc{user_name}] [%mdc{interface}] [%logger{0}] [%msg]%n
</pattern>
</encoder>
</appender>
<!-- ERROR级别日志单独输出 -->
<appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log4j.path}/error.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<maxHistory>3000</maxHistory>
<!-- 按时间回滚的同时,按文件大小来回滚 -->
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1GB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>
[%level] [%thread] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%mdc{traceid}] [%mdc{user_name}] [%mdc{interface}] [%logger{0}] [%msg]%n
</pattern>
</encoder>
<!-- 对ERROR级别的进行拦截 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 打印SQL日志 -->
<logger name="com.ibatis" additivity="true">
<level value="INFO" />
<appender-ref ref="sql"/>
</logger>
<logger name="java.sql.Connection" additivity="true">
<level value="INFO" />
<appender-ref ref="sql"/>
</logger>
<root level="INFO">
<appender-ref ref="root"/>
<!--<appender-ref ref="console"/>-->
</root>
出现的问题
Q:logback一直不生效
A:通过查看maven依赖树,发现在某个包中含有slf4j-log4j12的依赖,将这个依赖排除后问题解决。
网友评论