1.引入maven
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2.在/springboot/src/main/resources目录下,新建日志配置文件logback-spring.xml,如下:
配置如下
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<!--ch.qos.logback.core.ConsoleAppender 是 logback 类的完整路径-->
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
</pattern>
</layout>
</appender>
<!--RollingFileAppender 可以做到每天输出一个日志文件-->
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--LevelFilter -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<!--匹配则禁止-->
<onMatch>DENY</onMatch>
<!--不匹配则应用-->
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
</pattern>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--输出路径-->
<fileNamePattern>${user.dir}\logger\info.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--ThresholdFilter 根据范围过滤,即大于等于 error 的日志都记录-->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
</pattern>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${user.dir}\logger\error.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="consoleLog"></appender-ref>
<appender-ref ref="fileInfoLog"></appender-ref>
<appender-ref ref="fileErrorLog"></appender-ref>
</root>
</configuration>
3.测试代码
@Controller
@Slf4j
public class IndexController {
@RequestMapping("/test")
public void test() {
log.info("1111");
log.error("22222222");
System.out.println(1 / 0);
}
}
4.效果如下
info.2021-11-17.log如下:
error.2021-11-17.log如下: