java Spring boot - 备份日志文件的数量限制为 7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39610495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:30:47  来源:igfitidea点击:

Spring boot - number of backup log files restricted to 7

javaspringspring-bootlogbackslf4j

提问by Ravindra Devadiga

In our spring-bootproject we are using slf4jfor logging purpose. Below are configuration which we have added in application.propertiesfile

在我们的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

It generates only 7 backup files(my_log.log.1, my_log.log.2 ..., my_log.log.7) with each file of size 10.5MBand after that logging is not happening at all.

它只生成 7 个备份文件(my_log.log.1、my_log.log.2 ...、my_log.log.7),每个文件大小为10.5MB,之后日志记录根本不发生。

Is there any way to change this behavior?

有什么办法可以改变这种行为吗?

We looked into available properties of spring-boot but, didn't find anything. Any suggestion is appreciated.

我们查看了 spring-boot 的可用属性,但没有找到任何东西。任何建议表示赞赏。

回答by DeepakV

Spring-Bootonly allows limited properties to be configured in its application.properties. See the list here.

Spring-Boot只允许在其 application.properties 中配置有限的属性。请参阅此处列表

The default (out-of-the-box) configuration that Spring-boot uses is defined in base.xml. See base.xml config herewhich includes this File appender

Spring-boot 使用的默认(开箱即用)配置在 base.xml 中定义。请参阅此处的 base.xml 配置,其中包含此文件附加程序

There are 2 ways to add extra configuration

有两种方法可以添加额外的配置

  1. Add logback-spring.xml
  1. 添加 logback-spring.xml

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.

如果在项目的类路径中有一个名为 logback-spring.xml 的 logback 配置 XML,它会在初始化时被 Spring-Boot 选中。

  1. Point to config file from application.properties
  1. 从 application.properties 指向配置文件

Within application.properties use following to point to your custom logback XML

在 application.properties 中使用以下指向您的自定义 logback XML

logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback

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

使用上述 2 个步骤中的任何一个添加额外的配置后,可以在该自定义 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>

回答by Kozio?ek

SFL4J is just wrapper. You need to add extra configuration for logback library:

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>

source

来源

In this case we have logs from last 30 days but not bigger than 3GB.

在这种情况下,我们有过去 30 天的日志,但不超过 3GB。

回答by Frischling

for spring-boot 2.0.0:

对于 spring-boot 2.0.0:

  • logging.file.max-history
  • logging.file.max-size
  • logging.file.max-history
  • logging.file.max-size

...and others Either look at org.springframework.boot.logging.LoggingSystemPropertiesor https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/html/boot-features-logging.html#boot-features-logging-file-output

...和其他人要么看看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 -输出