因为运维要求单日志文件占磁盘空间不能太大,而我们这个本身的日志是按天来分割的, 所以现在得加上在原来的基础上再加上按文件大小分割,本来这是一个很简单的需求,改下日志配置就好了。
按照官方的文档,按时间和大小生成日志:
<appender name="ScanRollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/applogs/appweb/common.log</file>
<encoder>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%thread] %c.%M \(%F:%L\) %n%msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>/applogs/appweb/common.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<maxFileSize>3000MB</maxFileSize>
<MaxHistory>60</MaxHistory>
</rollingPolicy>
</appender>
- 按天分割
- 日志文件超过3G大小的也分割,所以 FileNamePattern 后面的
i%
不要忘记了 - 最大保留历史是60个
本来这样配置上去是没有问题的,能正常打印日志,但是某项目不生成 common.log 日志,也没有报错信息。 经过排查发现该项目使用的 logback-core
版本是1.1.2, SizeAndTimeBasedRollingPolicy
是在1.1.7才出来的.
March 29th, 2016, Release of version 1.1.7
Logback is now compact3 profile compatible. This improvement was requested in LOGBACK-1071 by Axel Fontaine with Max Urech providing the relevant pull-request.
Fixed ConcurrentModificationException being thrown when the reset() method is invoked on the LoggerContext instance. This issue was reported in LOGBACK-397 by Szczepan Faber with Ross Sargant providing the relevant test case.
TimeBasedRollingPolicy now supports the totalSizeCap property which allows the user to limit the total size of archived logs.
SizeAndTimeBasedRollingPolicy offers the same functionality as SizeAndTimeBasedFNATP did previously but with a simpler configuration structure.
Archive removal by RollingFileAppender is now performed asynchronously.
Unnecessary and incompatible %i token in fileNamePattern option with RollingFileAppender/TimeBasedRollingPolicy is now detected and the user alerted to the misconfiguration problem. This fixes LOGBACK-1143.
Joran can now handle logger names ending with a $, i.e. the first character in variable substitution. This issue was raised in LOGBACK-1149 by by Stevo Slavic.
链接: https://logback.qos.ch/news.html
所以,要么使用 SizeAndTimeBasedFNATP
来解决这个问题,要么升级 logback-core
的版本。