在我们的 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的可用属性,但没有找到任何东西。任何建议表示赞赏。

最佳答案

Spring-Boot 仅允许在application.properties中配置有限的属性。参见list here

Spring-boot使用的默认(开箱即用)配置在base.xml中定义。请参阅base.xml config here,其中包括this File appender

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

  • 添加logback-spring.xml

  • 如果项目的类路径中有一个名称为logback-spring.xml的logback配置XML,那么Spring-Boot将在初始化时使用它。
  • 指向application.properties中的配置文件

  • 在application.properties中,使用以下命令指向您的自定义登录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>
    

    09-10 12:53
    查看更多