在springboot应用程序中按大小或日期滚动日志文件

1. 本文的目的

我将演示如何在 springboot 应用程序中按日期或大小滚动日志文件。

2. 环境

  • 弹簧靴 1.x 和 2.x

3. 在 springboot 应用程序中滚动日志文件

3.1 春季的默认日志记录配置是什么?

默认情况下,springboot 使用logback作为默认记录器,但是如果您仅使用application.properties来配置日志记录,则存在一些限制:

logging.level.=ERROR  # this is the root logging level for all packages.
logging.level.com.test.sb1jt=DEBUG # this is the project-specific package logging level configuration
logging.file=springboot-logger.log # this is the logging target file

您可以看到,我们可以配置根日志记录级别和包日志记录级别,但是如果要按日期或大小滚动日志文件,该怎么办?

3.2 将登录配置文件添加到项目中

当 springboot 类路径中的文件具有以下名称之一时,Spring 引导将自动通过默认配置加载它:

  • logback-spring.xml
  • logback.xml
  • logback-spring.groovy
  • logback.groovy

建议使用倒数弹簧.xml。

3.3 按大小演示滚动日志文件

我们可以在 src/main/res/logback-spring 中定义滚动策略.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <!-- All log files located in logs file of the project -->
    <property name="LOGS" value="./logs" /> 

    <!-- Define the console log format -->
    <appender name="Console"
              class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFileBySize" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS}/spring-boot-loggerbysize.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>${LOGS}/spring-boot-loggerbysize-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <!-- each file should be at most 1MB, keep 6 days worth of history, but at most 3M -->
            <maxFileSize>1MB</maxFileSize>
            <maxHistory>6</maxHistory>
            <totalSizeCap>3MB</totalSizeCap>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
        </encoder>
    </appender>

    <!-- LOG everything at error level -->
    <root level="error">
        <appender-ref ref="RollingFileByDate" />
        <appender-ref ref="Console" />
    </root>

    <!-- LOG "com.test*" at TRACE level -->
    <logger name="com.test" level="trace" additivity="false">
        <appender-ref ref="RollingFileByDate" />
        <appender-ref ref="Console" />
    </logger>

</configuration>

然后,您可以通过运行打印大量日志的无限循环来进行测试。然后你会得到这些文件:

-rw-r--r--  1 zzz  staff   1.0M May 25 18:21 spring-boot-loggerbysize-2019-05-25.0.log
-rw-r--r--  1 zzz  staff   1.0M May 25 18:21 spring-boot-loggerbysize-2019-05-25.1.log
-rw-r--r--  1 zzz  staff   317K May 25 18:22 spring-boot-loggerbysize.log

3.4 按日期演示滚动日志文件

我们可以在 src/main/res/logback-spring 中定义日期滚动策略.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <!-- All log files located in logs file of the project -->
    <property name="LOGS" value="./logs" /> 

    <!-- Define the console log format -->
    <appender name="Console"
              class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFileByDate"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS}/spring-boot-loggerbyate.log</file>
        <encoder
                class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
        </encoder>

        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily and when the file reaches 10 MegaBytes -->
            <fileNamePattern>${LOGS}/spring-boot-loggerbydate-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>1MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <!-- LOG everything at error level -->
    <root level="error">
        <appender-ref ref="RollingFileByDate" />
        <appender-ref ref="Console" />
    </root>

    <!-- LOG "com.test*" at TRACE level -->
    <logger name="com.test" level="trace" additivity="false">
        <appender-ref ref="RollingFileByDate" />
        <appender-ref ref="Console" />
    </logger>

</configuration>

Then you can test by running a infinite loop which print a lot of logs. Then you would get these files:

-rw-r--r--  1 zzz  staff   393K May 25 18:34 spring-boot-loggerbyate.log
-rw-r--r--  1 zzz  staff   1.0M May 25 18:31 spring-boot-loggerbydate-2019-05-25.0.log
-rw-r--r--  1 zzz  staff   1.0M May 25 18:31 spring-boot-loggerbydate-2019-05-25.1.log
-rw-r--r--  1 zzz  staff   1.0M May 25 18:31 spring-boot-loggerbydate-2019-05-25.2.log
-rw-r--r--  1 zzz  staff   1.0M May 25 18:32 spring-boot-loggerbydate-2019-05-25.3.log
-rw-r--r--  1 zzz  staff   1.0M May 25 18:32 spring-boot-loggerbydate-2019-05-25.4.log
-rw-r--r--  1 zzz  staff   1.0M May 25 18:32 spring-boot-loggerbydate-2019-05-25.5.log
-rw-r--r--  1 zzz  staff   1.0M May 25 18:33 spring-boot-loggerbydate-2019-05-25.6.log
-rw-r--r--  1 zzz  staff   1.0M May 25 18:33 spring-boot-loggerbydate-2019-05-25.7.log
-rw-r--r--  1 zzz  staff   1.0M May 25 18:33 spring-boot-loggerbydate-2019-05-25.8.log
-rw-r--r--  1 zzz  staff   1.0M May 25 18:34 spring-boot-loggerbydate-2019-05-25.9.log

4. 结论

可以通过阅读日志日志记录追加程序配置来获取有关回日志追加程序的详细信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值