使用 spring 启动的 Logback TimeBasedRollingPolicy
Logback TimeBasedRollingPolicy with spring boot
我写了一个 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)。请注意,我确实想继续使用 logging.path 和 logging.file 来自 application.properties因为我根据环境有多个
谢谢
1) 您需要设置logging.file
或logging.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>
当当前日志文件为 rolled/archived 时,您会自动获得此格式,因为已达到模式定义的时间限制。在你的情况下,这意味着每天。
我写了一个 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)。请注意,我确实想继续使用 logging.path 和 logging.file 来自 application.properties因为我根据环境有多个
谢谢
1) 您需要设置logging.file
或logging.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>
当当前日志文件为 rolled/archived 时,您会自动获得此格式,因为已达到模式定义的时间限制。在你的情况下,这意味着每天。