Spring Boot-备份日志文件数限制为7 [英] Spring boot - number of backup log files restricted to 7
问题描述
在我们的 spring-boot 项目中,我们使用 slf4j 进行记录.以下是我们在 application.properties 文件
In our spring-boot project we are using slf4j for logging purpose. Below are configuration which we have added in application.properties file
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 之后,根本就不会进行日志记录了.
It generates only 7 backup files (my_log.log.1, my_log.log.2 ..., my_log.log.7) with each file of size 10.5MB and after that logging is not happening at all.
有什么办法可以改变这种行为?
Is there any way to change this behavior?
我们查看了 spring-boot 的可用属性,但没有找到任何东西.任何建议表示赞赏.
We looked into available properties of spring-boot but, didn't find anything. Any suggestion is appreciated.
推荐答案
Spring-Boot 仅允许在application.properties中配置有限的属性.请参阅此处的列表.
Spring-Boot only allows limited properties to be configured in its application.properties. See the list here.
Spring-boot使用的默认(开箱即用)配置在base.xml中定义.参见此处的base.xml配置,其中包括
The default (out-of-the-box) configuration that Spring-boot uses is defined in base.xml. See base.xml config here which includes this File appender
有2种方法可以添加额外的配置
There are 2 ways to add extra configuration
- 添加logback-spring.xml
如果项目的类路径中有一个名称为logback-spring.xml的登录配置XML,则在初始化时由Spring-Boot拾取.
If there is a logback configuration XML with name logback-spring.xml in project's classpath, it is picked up by Spring-Boot on initialization.
- 从application.properties指向配置文件
在application.properties中,使用以下代码指向您的自定义登录XML
Within application.properties use following to point to your custom logback XML
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
一旦使用以上两个步骤中的任何一个添加了额外的配置,就可以在这种自定义XML中提及过渡策略
Once you add the extra config using any of the above 2 steps, the rollover strategy can be mentioned within that custom XML like this
<?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-备份日志文件数限制为7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!