Spring boot - 备份日志文件数量限制为 7
Spring boot - number of backup log files restricted to 7
在我们的 spring-boot 项目中,我们使用 slf4j 进行日志记录。以下是我们在 application.properties 文件
中添加的配置
logging.file=/opt/logs/my_log.log
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
logging.level.nl.yestelecom.boss=DEBUG
logging.level.com.github.isrsal.logging.LoggingFilter=DEBUG
它仅生成 7 个备份文件 (my_log.log.1, my_log.log.2 ..., my_log.log.7)每个大小为 10.5MB 的文件,此后根本不会进行日志记录。
有什么办法可以改变这种行为吗?
我们查看了 spring-boot 的可用属性,但是没有找到任何东西。任何建议表示赞赏。
SFL4J 只是包装器。您需要为 logback 库添加额外的配置:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history capped at 3GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
在这种情况下,我们有过去 30 天的日志,但不超过 3GB。
Spring-Boot只允许在其application.properties中配置有限的属性。参见list here。
Spring-boot 使用的默认(开箱即用)配置在 base.xml 中定义。参见 base.xml config here which includes this File appender
有两种方法可以添加额外的配置
- 添加logback-spring.xml
如果在项目的类路径中有名称为 logback-spring.xml 的 logback 配置 XML,它会在初始化时被 Spring-Boot 拾取。
- 从 application.properties
指向配置文件
在 application.properties 中使用以下指向您的自定义 logback XML
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
一旦您使用上述两个步骤中的任何一个添加了额外的配置,就可以在该自定义 XML 中提及翻转策略,就像这样
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<Pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
</encoder>
</appender>
<appender name="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.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE"/>
</root>
</configuration>
对于 spring-boot 2.0.0:
- logging.file.max-history
- logging.file.max-size
...以及其他
看看 org.springframework.boot.logging.LoggingSystemProperties
或者
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/html/boot-features-logging.html#boot-features-logging-file-output
在我们的 spring-boot 项目中,我们使用 slf4j 进行日志记录。以下是我们在 application.properties 文件
中添加的配置logging.file=/opt/logs/my_log.log
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
logging.level.nl.yestelecom.boss=DEBUG
logging.level.com.github.isrsal.logging.LoggingFilter=DEBUG
它仅生成 7 个备份文件 (my_log.log.1, my_log.log.2 ..., my_log.log.7)每个大小为 10.5MB 的文件,此后根本不会进行日志记录。
有什么办法可以改变这种行为吗?
我们查看了 spring-boot 的可用属性,但是没有找到任何东西。任何建议表示赞赏。
SFL4J 只是包装器。您需要为 logback 库添加额外的配置:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history capped at 3GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
在这种情况下,我们有过去 30 天的日志,但不超过 3GB。
Spring-Boot只允许在其application.properties中配置有限的属性。参见list here。
Spring-boot 使用的默认(开箱即用)配置在 base.xml 中定义。参见 base.xml config here which includes this File appender
有两种方法可以添加额外的配置
- 添加logback-spring.xml
如果在项目的类路径中有名称为 logback-spring.xml 的 logback 配置 XML,它会在初始化时被 Spring-Boot 拾取。
- 从 application.properties 指向配置文件
在 application.properties 中使用以下指向您的自定义 logback XML
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
一旦您使用上述两个步骤中的任何一个添加了额外的配置,就可以在该自定义 XML 中提及翻转策略,就像这样
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<Pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
</encoder>
</appender>
<appender name="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.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE"/>
</root>
</configuration>
对于 spring-boot 2.0.0:
- logging.file.max-history
- logging.file.max-size
...以及其他
看看 org.springframework.boot.logging.LoggingSystemProperties
或者
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/html/boot-features-logging.html#boot-features-logging-file-output