sprinboot系列三——接入log4j2

本文介绍如何在Spring Boot中配置Log4j2实现异步日志记录,并利用lmax disruptor进行性能优化。通过排除Spring默认的日志包,引入log4j2及disruptor依赖,设置log4j2.xml配置文件,实现按日滚动和日志级别过滤。

导入依赖

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());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值