log4j2.xml全异步日志配置,保存15天日志,info/error最多有7个压缩的日志文件(日志达到大小触发压缩机制)。
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
log4j2.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn" monitorInterval="60" strict="true">
<Properties>
<Property name="base.dir">logs</Property>
<Property name="info.file.name">${base.dir}/info</Property>
<Property name="error.file.name">${base.dir}/error</Property>
<Property name="layout.pattern">[%d{yyyy-MM-dd HH:mm:ss,SSS} %5p] [%t] [%c{2}]- %m%n</Property>
<Property name="file.max.age">15d</Property>
<Property name="log_info_level">info</Property>
<Property name="log_debug_level">debug</Property>
<Property name="log_error_level">error</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout charset="UTF-8" pattern="${layout.pattern}"/>
</Console>
<RollingFile name="FILE_INFO" fileName="${info.file.name}.log"
filePattern="${info.file.name}.%d{yyyy-MM-dd}-%i.log.gz">
<Filters>
<ThresholdFilter level="${log_debug_level}" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout charset="UTF-8" pattern="${layout.pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="1G"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${base.dir}" maxDepth="1">
<IfFileName glob="${info.file.name}.*.log.gz"/>
<IfLastModified age="${file.max.age}"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="FILE_ERROR" fileName="${error.file.name}.log"
filePattern="${error.file.name}.%d{yyyy-MM-dd}-%i.gz">
<Filters>
<ThresholdFilter level="${log_error_level}" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout charset="UTF-8" pattern="${layout.pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="200MB"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${base.dir}" maxDepth="1">
<IfFileName glob="${error.file.name}.*.gz"/>
<IfLastModified age="${file.max.age}"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<AsyncLogger name="com" level="DEBUG" additivity="false">
<!--${mvn.log.appenders}-->
<AppenderRef ref="FILE_INFO"/>
<AppenderRef ref="FILE_ERROR"/>
</AsyncLogger>
<AsyncRoot level="INFO">
<!--${mvn.log.appenders}-->
<AppenderRef ref="FILE_INFO"/>
<AppenderRef ref="FILE_ERROR"/>
</AsyncRoot>
</Loggers>
</configuration>