使用Spring Boot配置Log4j2.properties

时间:2017-04-11 20:06:50

标签: spring-boot log4j2

Spring Boot 1.5.2是否可以使用log4j2属性配置而不是xml?

官方spring文档上的日志文档似乎表明只支持xml。

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

2 个答案:

答案 0 :(得分:7)

是的,你可以。就我而言,我无法通过Spring Boot找到我的log4j2.properties。我必须在application.properties内指定:

logging.config=src/main/resources/log4j2.properties

如果我在同一个文件夹中有log4j2.xml,我就不必这样做

答案 1 :(得分:0)

是的,有可能。我在使用Spring Boot 1.5.2的微服务环境中工作,并用于记录LOG4j2。

首先,您需要删除spring日志记录功能并添加log4j2依赖项。

 <dependency>
    <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</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>

创建log4j2.properties文件并声明所有必需的配置,例如:

name = ABC 
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ABClog/ABCapplication.log
appender.rolling.filePattern = ABClog/ABCapplication.%d{dd-MMM}.log
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
appender.rolling.strategy.type = DefaultRolloverStrategy 
appender.rolling.strategy.action.type = Delete 
appender.rolling.strategy.action.basepath =log 
appender.rolling.strategy.action.condition.type = IfFileName
appender.rolling.strategy.action.condition.glob = ABC*.log
appender.rolling.strategy.action.ifAny.type = IfAny
appender.rolling.strategy.action.ifAny.ifLastModified.type = IfLastModified
appender.rolling.strategy.action.ifAny.ifLastModified.age = 14d
appender.rolling.strategy.action.ifAny.ifAccumulatedFileSize.type = IfAccumulatedFileSize
appender.rolling.strategy.action.ifAny.ifAccumulatedFileSize.exceeds = 200MB
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
logger.rolling.name = com.currentobject.controller
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

将lo4j42.properties文件的路径提供到application.properties文件(资源文件夹)中

logging.config=classpath:log4j2.properties

在application.properties文件中注释Spring引导的默认日志记录属性:

#logging.level.org.springframework.web=DEBUG