springboot集成log4j2

在创建Spring Boot工程时,我们引入了spring-boot-starter,其中包含了spring-boot-starter-logging,该依赖内容就是Spring Boot默认的日志框架Logback,所以我们在引入log4j之前,需要先排除该包的依赖,再引入log4j的依赖



    org.springframework.boot
    spring-boot-starter-web
    
      
        ch.qos.logback
        logback-classic
      
      
        org.springframework.boot
        spring-boot-starter-logging
      
    


    org.springframework.boot
    spring-boot-starter-log4j2
    2.2.4.RELEASE

使用

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
public class SampleController {

    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    
    @GetMapping(value = "/sample/testlog")
    @ResponseBody
    Object testlog() {
        logger.info("统计异常池数量异常,异常信息如下:e.getStackTrace().toString()");
        return "ok";
    }
}

配置log4j2
在resources文件夹下新建log4j2.properties文件
在application.properties中指定log4j2配置文件地址

logging.config=classpath:log4j2.properties

log4j2.properties中的配置如下


# 只记录debug级别以上的日志,大小写无关
filter.threshold.type = ThresholdFilter
filter.threshold.level = info
 
appenders = console,I, W,D

#地址这里写的是绝对地址.logs文件夹会自动创建的,不用自己去手动创建
#公共变量
property.LOG_HOME=/Users/mobile_4/Desktop/demo/logs

appender.console.type = Console
appender.console.name = STDOUT
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %-d{yyyy-MM-dd HH:mm:ss}  [%t] [ %c ] [ %L ] [ %p ] %m%n
 
appender.I.type = RollingFile
appender.I.name = InfoRollingFile
appender.I.fileName = ${LOG_HOME}/info/info.log
appender.I.filePattern = ${LOG_HOME}/info/info_%d{yyyy-MM-dd}.log.%i
appender.I.layout.type = PatternLayout
appender.I.layout.pattern = %-d{yyyy-MM-dd HH:mm:ss}  [%t] [ %c ] [ %L ] [ %p ] %m%n
appender.I.policies.type = Policies
appender.I.policies.time.type = TimeBasedTriggeringPolicy
appender.I.policies.time.interval = 1
appender.I.policies.time.modulate = true
appender.I.policies.size.type = SizeBasedTriggeringPolicy
appender.I.policies.size.size=100M
appender.I.strategy.type = DefaultRolloverStrategy
appender.I.strategy.max = 10000
appender.I.filter.threshold.type = ThresholdFilter
appender.I.filter.threshold.level = WARN
appender.I.filter.threshold.onMatch = DENY
appender.I.filter.threshold.onMisMatch=NEUTRAL
 
 
appender.D.type = RollingFile
appender.D.name = DebugRollingFile
appender.D.fileName =  ${LOG_HOME}/debug/debug.log
appender.D.filePattern =  ${LOG_HOME}/debug/debug_%d{yyyy-MM-dd}.log.%i
appender.D.layout.type = PatternLayout
appender.D.layout.pattern = %-d{yyyy-MM-dd HH:mm:ss}  [%t] [ %c ] [ %L ] [ %p ] %m%n
appender.D.policies.type = Policies
appender.D.policies.time.type = TimeBasedTriggeringPolicy
appender.D.policies.time.interval = 1
appender.D.policies.time.modulate = true
appender.D.policies.size.type = SizeBasedTriggeringPolicy
appender.D.policies.size.size=100M
appender.D.strategy.type = DefaultRolloverStrategy
appender.D.strategy.max = 10000

appender.W.type = RollingFile
appender.W.name = ErrorRollingFile
appender.W.fileName =  ${LOG_HOME}/error/error.log
appender.W.filePattern =  ${LOG_HOME}/error/error_%d{yyyy-MM-dd}.log.%i
appender.W.layout.type = PatternLayout
appender.W.layout.pattern = %-d{yyyy-MM-dd HH:mm:ss}  [%t] [ %c ] [ %L ] [ %p ] %m%n
appender.W.policies.type = Policies
appender.W.policies.time.type = TimeBasedTriggeringPolicy
appender.W.policies.time.interval = 1
appender.W.policies.time.modulate = true
appender.W.policies.size.type = SizeBasedTriggeringPolicy
appender.W.policies.size.size=100M
appender.W.strategy.type = DefaultRolloverStrategy
appender.W.strategy.max = 10000
 
 
rootLogger.level = debug
rootLogger.appenderRefs = stdout,I,W,D
#rootLogger.appenderRefs =I, W
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.D.ref = DebugRollingFile
rootLogger.appenderRef.D.level = debug
rootLogger.appenderRef.I.ref = InfoRollingFile
rootLogger.appenderRef.I.level = info
rootLogger.appenderRef.W.ref = ErrorRollingFile
rootLogger.appenderRef.W.level = error

你可能感兴趣的:(springboot集成log4j2)