一 添加依赖
<!--移除spring-boot中默认的logback日志组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--依赖log4j2组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-spring-boot</artifactId>
</dependency>
<!--支持slf4j日志api-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>
二 log4j2.xml日志模板
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration name="XMLCommonLog" status="WARN" strict="true" packages="io.opentelemetry.instrumentation.log4j.appender.v2_17">
<Properties>
<Property name="application.name" >log4jDemo</Property>
<!-- <Property name="user.home" >/test/service</Property>-->
<!-- <Property name="application.name" >${spring:spring.application.name}</Property>-->
<!-- <Property name="user.home" >${spring:user.home}</Property>-->
<Property name="user.home" >I:/springBoot/log4jDemo</Property>
<Property name="LOG_PATH" value="${user.home}/logs/${application.name}" />
<Property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%X{trace_id}] [%t] %C{1.}.%M:%L - %msg%n" />
<Property name="TRACING_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%X{trace_id}] [%t] %C{1.}.%M:%L - %msg%n" />
</Properties>
<Appenders>
<RollingFile name="WARN" fileName="${LOG_PATH}/warn.log" filePattern="${LOG_PATH}/warn-%d{yyyy-MM-dd}-%i.log"
immediateFlush="false">
<PatternLayout pattern="${TRACING_PATTERN}" />
<LevelRangeFilter minLevel="WARN" maxLevel="WARN" />
<Policies>
<!--基于文件大小的滚动策略-->
<SizeBasedTriggeringPolicy size="500MB" />
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
<!--同一天内日志文件策略只保留7个-->
<DefaultRolloverStrategy max="7">
<!--制定策略保留7天内的日志文件-->
<Delete basePath="${LOG_PATH}" maxDepth="1">
<IfFileName glob="*.log" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="ERROR" fileName="${LOG_PATH}/error.log" filePattern="${LOG_PATH}/error-%d{yyyy-MM-dd}-%i.log"
immediateFlush="false">
<PatternLayout pattern="${TRACING_PATTERN}" />
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY" />
<Policies>
<SizeBasedTriggeringPolicy size="500MB" />
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
<DefaultRolloverStrategy max="7">
<Delete basePath="${LOG_PATH}" maxDepth="1">
<IfFileName glob="*.log" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<RollingFile name="INFO" fileName="${LOG_PATH}/info.log" filePattern="${LOG_PATH}/info-%d{yyyy-MM-dd}-%i.log"
immediateFlush="false">
<PatternLayout pattern="${TRACING_PATTERN}" />
<LevelRangeFilter minLevel="INFO" maxLevel="INFO"/>
<Policies>
<SizeBasedTriggeringPolicy size="500MB" />
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
<DefaultRolloverStrategy max="7">
<Delete basePath="${LOG_PATH}" maxDepth="1">
<IfFileName glob="*.log" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<loggers>
<!-- 这里采用log4j2的异步日志输出方式,来提高性能 -->
<AsyncRoot level="info" includeLocation="true">
<AppenderRef ref="CONSOLE"/>
<AppenderRef ref="INFO" />
<AppenderRef ref="WARN" />
<AppenderRef ref="ERROR" />
</AsyncRoot>
</loggers>
</Configuration>
添加log4j2.component.properties配置文件
# 当日志没有时,添加Block来避免CPU空转
AsyncLoggerConfig.WaitStrategy=Block
AsyncLoggerConfig.SynchronizeEnqueueWhenQueueFull=false
三配置日志
代码地址
Log4j2的Policies详解
Log4j2的DefaultRolloverStrategy详解