1、问题描述
使用logback持久化记录日志。
2、我的代码
logback是Springboot框架里自带的,所以只要引入“spring-boot-starter”就行了。无需额外引入logback依赖。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>TestLogBack</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><springframework.version>2.2.1.RELEASE</springframework.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version><scope>compile</scope></dependency></dependencies>
</project>
logback-spring.xml
(带“-spirng”的配置文件则无需在application.yml里面在配置logging:config)
<?xml version="1.0" encoding="UTF-8"?>
<configuration><include resource="org/springframework/boot/logging/logback/defaults.xml" /><include resource="org/springframework/boot/logging/logback/console-appender.xml" /><!-- 开启后可以通过jmx动态控制日志级别(springboot Admin的功能) --><!--<jmxConfigurator/>--><springProperty scope="context" name="log.path" source="logging.path" defaultValue="./logs"/><appender name="common-all" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- <File>${log.path}/api/common-all.log</File>--><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><FileNamePattern>${log.path}/api/common-all/common-all.%d{yyyy-MM-dd}.log.gz</FileNamePattern><maxHistory>30</maxHistory><!-- 保留30天日志 --></rollingPolicy><layout class="ch.qos.logback.classic.PatternLayout"><pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] [%X{klone.requestFlowNo}|%X{klone.consumerIp}] %logger{56}.%method\(\): - %msg%n</pattern></layout></appender><appender name="common-error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- <File>${log.path}/api/common-error.log</File>--><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><FileNamePattern>${log.path}/api/common-error/common-error.%d{yyyy-MM-dd}.log.gz</FileNamePattern><maxHistory>30</maxHistory><!-- 保留30天日志 --></rollingPolicy><layout class="ch.qos.logback.classic.PatternLayout"><pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] [%X{klone.requestFlowNo}|%X{klone.consumerIp}] %logger{56}.%method\(\): - %msg%n</pattern></layout><filter class="ch.qos.logback.classic.filter.LevelFilter"><level>ERROR</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter></appender><root level="INFO"><appender-ref ref="CONSOLE"/><appender-ref ref="common-all"/><appender-ref ref="common-error"/></root>
</configuration>
TestLogbackController.java
package com.mytest.controller;import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author: Wulc* @time: 2024/5/10 21:54* @description:*/
@RestController
@RequestMapping("/test")
@Slf4j
public class TestLogbackController {//private Logger logger = LoggerFactory.getLogger(getClass());@GetMapping("/printLog")public String printLog(int i) {//logger.info("打印一条日志");log.info("打印一条日志");int k = 10 / i;return "打印了日志";}
}
3、效果
4、总结
写这篇文章,主要是记录一下logback-spring.xml的配置内容。
5、参考资料
https://chatgpt.com/?oai-dm=1