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. 结论
可以通过阅读日志日志记录追加程序配置来获取有关回日志追加程序的详细信息。