【问题标题】:Logback TimeBasedRollingPolicy with spring boot使用 Spring Boot 的 Logback TimeBasedRollingPolicy
【发布时间】:2019-07-27 09:40:52
【问题描述】:

我写了一个 TimeBasedRollingPolicy logback.xml,虽然成功创建了日志文件,但它似乎是从 application.properties 读取它。这是我的设置:

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

    <appender name="ROLLING-FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${LOG_FILE}-%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ROLLING-FILE"/>
    </root>

</configuration>

application.properties

logging.path=/path/to/log/folder/
logging.file=${logging.path}myLog
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

当我运行我的应用程序时,日志已成功保存在正确的路径中,但仅保存为 myLog。我希望它附加日期(如在 logback.xml 中)。注意我确实想继续从 application.properties 中获取 logging.pathlogging.file 因为我有多个取决于环境。

谢谢

【问题讨论】:

标签: java spring-boot logback slf4j spring-logback


【解决方案1】:

1) 您需要设置 logging.filelogging.path 属性,不能同时设置。
所以logging.file=/path/to/log/folder/myLog应该足够在指定路径下得到myLog的日志文件了。

The spring boot documentation 提到了这一点。

2) 这是 rolling 日志的格式模式,而不是 current 日志的格式模式:

 <fileNamePattern>${LOG_FILE}-%d{yyyy-MM-dd}.log</fileNamePattern>  

当当前日志文件滚动/归档时,您会自动获得此格式,因为已达到模式定义的时间限制。在你的情况下,这意味着每天。

【讨论】:

  • 谢谢,尽管稍微澄清一下,鉴于我的 logback.xml,我必须在 application.properties 中设置 logging.file 而不是 logging.path,因为 logback 文件引用了 LOG_FILE。
  • @Tiberiu 当然。这是我的一个错误。谢谢你的精确。我更新以使其更清晰。
猜你喜欢
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2019-01-19
  • 1970-01-01
  • 2019-02-15
  • 2016-04-24
  • 2020-07-18
相关资源
最近更新 更多