【问题标题】:Configuring Log4j2.properties with Spring Boot使用 Spring Boot 配置 Log4j2.properties
【发布时间】:2017-09-07 09:26:12
【问题描述】:

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

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

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

【问题讨论】:

    标签: spring-boot log4j2


    【解决方案1】:

    是的,这是可能的。我正在使用 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 boot的默认日志属性:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2018-06-12
        • 2019-10-26
        • 1970-01-01
        • 2018-06-14
        • 2019-05-19
        • 2019-08-31
        • 2019-01-24
        • 1970-01-01
        • 2018-10-13
        相关资源
        最近更新 更多