环境
- 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