导入依赖
log4j2依赖lmax完成异步日志,排除spring本身log包
<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>
<!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
添加配置
application.properties里面声明日志
logging.config= classpath:log4j2.xml
springboot默认扫描目录如下:
classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources
根目录,添加log4j2.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<!-- \u53d8\u91cf\u914d\u7f6e -->
<Properties>
<Property name="log_path">./logs</Property>
</Properties>
<!-- appender\u914d\u7f6e -->
<!-- uuid用于标识系列日志 -->
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss} %5p %X{uuid} -- %m%n" />
</Console>
<RollingFile name="TEST.SYSTEM" fileName="${log_path}/system.log"
filePattern="${log_path}/system.log.%d{yyyy-MM-dd}">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss} %5p %X{uuid} -- %m%n" />
<Policies>
<CronTriggeringPolicy schedule="0 0 0 * * ? *" />
</Policies>
<Filters>
<ThresholdFilter level="warn" onMatch="DENY"
onMismatch="NEUTRAL" />
<ThresholdFilter level="info" onMatch="ACCEPT"
onMismatch="NEUTRAL" />
</Filters>
<DefaultRolloverStrategy max="7">
<Delete basePath="${log_path}" maxDepth="3">
<IfFileName glob="*.log.*" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="TEST.ERROR" fileName="${log_path}/error.log"
filePattern="${log_path}/error.log.%d{yyyy-MM-dd}">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss} %5p %X{uuid} -- %m%n" />
<Policies>
<CronTriggeringPolicy schedule="0 0 0 * * ? *" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT"
onMismatch="NEUTRAL" />
<ThresholdFilter level="trace" onMatch="DENY"
onMismatch="NEUTRAL" />
</Filters>
<DefaultRolloverStrategy max="7"/>
</RollingFile>
</Appenders>
<Loggers>
<AsyncLogger name="com.test" level="info" additivity="false">
<AppenderRef ref="TEST.SYSTEM" />
<AppenderRef ref="TEST.ERROR" />
<AppenderRef ref="Console" level="info" />
</AsyncLogger>
<Root level="info">
<AppenderRef ref="Console" level="info" />
<AppenderRef ref="TEST.SYSTEM" />
<AppenderRef ref="TEST.ERROR" />
</Root>
</Loggers>
</Configuration>
接入uuid
在全局拦截类里面,将生成的uuid放入即可
MDC.clear();
MDC.put("uuid", StringUtils.getUUID());