先看效果---->>>>
按照:error、info、warn进行分类:
每个文件里面按日期进行分类:
其中对应的Maven如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml中添加以下内容:
#日志配置
logging:
file:
path: logs #content root目录下的logs文件夹存放日志,不存在则创建
pattern:
console: "%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n"
核心文件:logback-spring.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds">
<springProperty scope="context" name="LOG_NAME" source="spring.application.name"/>
<springProperty scope="context" name="LOG_PATH" source="logging.file.path"/>
<!-- 日志属性配置 -->
<property name="LOG_FILE" value="${LOG_PATH}/${LOG_NAME}.log"/>
<property name="LOG_FILE_CLEAN_HISTORY_ON_START" value="false" />
<property name="ROLLING_FILE_NAME_PATTERN" value="${LOG_FILE}.%d{yyyy-MM-dd}.%i" />
<property name="LOG_FILE_MAX_SIZE" value="512MB" />
<property name="LOG_FILE_MAX_HISTORY" value="30" />
<contextName>${LOG_NAME}</contextName>
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出(配色):%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%boldMagenta(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %cyan(%logger{50}) - %msg %n</pattern>
</encoder>
</appender>
<!--根据日志级别分离日志,分别输出到不同的文件-->
<appender name="debugLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>debug</level>
</filter>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n
</pattern>
<charset>UTF-8</charset>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--按时间保存日志 修改格式可以按小时、按天、月来保存-->
<fileNamePattern>${LOG_PATH}/${LOG_NAME}-debug.%d{yyyy-MM-dd}.log</fileNamePattern>
<!--保存时长-->
<MaxHistory>${LOG_FILE_MAX_HISTORY}</MaxHistory>
<!--文件大小-->
<totalSizeCap>${LOG_FILE_MAX_SIZE}</totalSizeCap>
</rollingPolicy>
</appender>
<!--根据日志级别分离日志,分别输出到不同的文件-->
<appender name="infoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n
</pattern>
<charset>UTF-8</charset>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--按时间保存日志 修改格式可以按小时、按天、月来保存-->
<fileNamePattern>${LOG_PATH}/${LOG_NAME}-info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!--保存时长-->
<MaxHistory>${LOG_FILE_MAX_HISTORY}</MaxHistory>
<!--文件大小-->
<totalSizeCap>${LOG_FILE_MAX_SIZE}</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="errorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n
</pattern>
<charset>UTF-8</charset>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>${LOG_PATH}/${LOG_NAME}-error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!--保存时长-->
<MaxHistory>${LOG_FILE_MAX_HISTORY}</MaxHistory>
<!--文件大小-->
<totalSizeCap>${LOG_FILE_MAX_SIZE}</totalSizeCap>
</rollingPolicy>
</appender>
<!--如果不需要区分spring环境,直接用下边代码-->
<root level="info">
<appender-ref ref="consoleLog"/>
<appender-ref ref="infoLog"/>
<appender-ref ref="errorLog"/>
</root>
<!--如果需要区分springfile 需要用下边代码-->
<!-- <springProfile name="local">
<root level="debug">
<appender-ref ref="consoleLog"/>
</root>
</springProfile>
<springProfile name="dev">
<root level="info">
<appender-ref ref="consoleLog"/>
<appender-ref ref="infoLog"/>
<appender-ref ref="errorLog"/>
<appender-ref ref="JSON_FILE" />
</root>
</springProfile>
<springProfile name="test">
<root level="info">
<appender-ref ref="infoLog"/>
<appender-ref ref="errorLog"/>
<appender-ref ref="JSON_FILE" />
</root>
</springProfile>
<springProfile name="prod">
<root level="info">
<appender-ref ref="consoleLog"/>
<appender-ref ref="infoLog"/>
<appender-ref ref="errorLog"/>
<appender-ref ref="JSON_FILE" />
</root>
</springProfile>-->
</configuration>