如上图,每天会生成一个新的日志文件,然后日志进行分类,我这里只对error和info进行分类。
怎么做呢?
首先,在resource目录创建一个新文件,取名logback-spring.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <configuration >
- <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
- <layout class="ch.qos.logback.classic.PatternLayout">
- <pattern>
- %d - %msg%n
- </pattern>
- </layout>
- </appender>
- <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <filter class="ch.qos.logback.classic.filter.LevelFilter">
- <level>
- ERROR
- </level>
- <onMatch>DENY</onMatch>
- <onMismatch>ACCEPT</onMismatch>
- </filter>
- <encoder>
- <pattern>
- %msg%n
- </pattern>
- </encoder>
- <!-- 滚动策略-->
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <!-- 路径-->
- <fileNamePattern>
- /var/log/tomcat/sell/info.%d.log
- </fileNamePattern>
- </rollingPolicy>
- </appender>
- <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
- <level>
- ERROR
- </level>
- </filter>
- <encoder>
- <pattern>
- %msg%n
- </pattern>
- </encoder>
- <!-- 滚动策略-->
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <!-- 路径-->
- <fileNamePattern>
- /var/log/tomcat/sell/error.%d.log
- </fileNamePattern>
- </rollingPolicy>
- </appender>
- <root level="info">
- <appender-ref ref="consoleLog" />
- <appender-ref ref="fileInfoLog" />
- <appender-ref ref="fileErrorLog" />
- </root>
- </configuration>
单例测试:
- import lombok.Data;
- import lombok.extern.slf4j.Slf4j;
- import org.junit.jupiter.api.Test;
- import org.junit.runner.RunWith;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
- @RunWith(SpringRunner.class)
- @SpringBootTest
- @Slf4j
- @Data
- public class LoggerTest {
- @Test
- public void test1(){
- String name="laomi";
- String password="123456";
- log.info("debug.....");
- log.info("info.....");
- log.error("error....");
- log.info("name:{}, password:{}",name,password);
- }
- }
添加依赖:
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <scope>test</scope>
- </dependency>
fileNamePattern这个是文件路径,我是在这个项目的同级目录找到新创建的文件夹的
以此文章以后方便二次使用。
到此这篇关于Springboot项目优雅地处理日志的文章就介绍到这了,更多相关Springboot处理日志内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_43449246/article/details/118511899