log4j2 实现error级别以上日志输出到特定日志文件

环境

  • springboot
  • log4j2.propertis
  • maven

实现需求

@slf4j有error方法,好像是没有fatal
@log4j都有

实现一个某一个日志等级以上得日志除了,正常默认日志中有,另外的日志中也会有,有利于error日志的统一管理。

maven依赖

<!--		详细配置log4j.properties-->
		<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>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-log4j2</artifactId>
		</dependency>

spring boot配置依赖指定日志配置文件

logging:
  config: classpath:log4j2.properties

日志文件内容

status = info
name= properties_configuration

# Give directory path where log files should get stored
property.basePath = ./log/

# ConsoleAppender will print logs on console
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = info
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout

# Specify the pattern of the logs
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg%n


# RollingFileAppender will print logs in file which can be rotated based on time or size
appender.rolling.type = RollingFile
appender.rolling.name = errorLogger
appender.rolling.filter.threshold.type = ThresholdFilter
appender.rolling.filter.threshold.level = error
appender.rolling.fileName= ${basePath}error.log
appender.rolling.filePattern= ${basePath}error_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg%n
appender.rolling.policies.type = Policies

# Rotate log file each day and keep 30 days worth
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.delete.type = Delete
appender.rolling.strategy.delete.basePath = ${basePath}
appender.rolling.strategy.delete.maxDepth = 1
appender.rolling.strategy.delete.ifLastModified.type = IfLastModified
# Delete files older than 30 days
appender.rolling.strategy.delete.ifLastModified.age = 30d




# RollingFileAppender will print logs in file which can be rotated based on time or size
appender.infos.type = RollingFile
appender.infos.name = fileLogger
appender.infos.filter.threshold.type = ThresholdFilter
appender.infos.filter.threshold.level = info
appender.infos.fileName= ${basePath}log.log
appender.infos.filePattern= ${basePath}log_%d{yyyyMMdd}.log.gz
appender.infos.layout.type = PatternLayout
appender.infos.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg%n
appender.infos.policies.type = Policies

# Rotate log file each day and keep 30 days worth
appender.infos.policies.time.type = TimeBasedTriggeringPolicy
appender.infos.policies.time.interval = 1
appender.infos.policies.time.modulate = true
appender.infos.strategy.type = DefaultRolloverStrategy
appender.infos.strategy.delete.type = Delete
appender.infos.strategy.delete.basePath = ${basePath}
appender.infos.strategy.delete.maxDepth = 1
appender.infos.strategy.delete.ifLastModified.type = IfLastModified
# Delete files older than 30 days
appender.infos.strategy.delete.ifLastModified.age = 30d

# Configure root logger for logging error logs in classes which are in package other than above specified package
rootLogger.level = info
rootLogger.additivity = false
rootLogger.appenderRef.rolling.ref = errorLogger
rootLogger.appenderRef.infos.ref = fileLogger
rootLogger.appenderRef.console.ref = consoleLogger
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值