-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
When using SizeAndTimeBasedRollingPolicy:
- Let's say I would like the current log file to be archived as a compressed file with an increasing index, starting at 0, at the start of the day and each time it reaches 200MB.
- At the same time, I would like to keep 5 days worth of compressed archive files, but at most 100MB of archive files in total.
I would think that the following config should work:
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>myapp.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>myapp-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxFileSize>200MB</maxFileSize>
<maxHistory>5</maxHistory>
<totalSizeCap>100MB</totalSizeCap>
</rollingPolicy>
</appender>However, this causes the following error:
Exception in thread "main" java.lang.IllegalStateException: java.lang.IllegalStateException: Logback configuration error detected:
ERROR in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@1392570698 - totalSizeCap of [100 MB] is smaller than maxFileSize [200 MB] which is non-sensical
This error makes sense if the archive files are not compressed. However, compressed archive files are much smaller than the uncompressed log file, so when archiving compressed log files, the total size of all archive files can actually be smaller than the maximum log file size.
Looking at the code in TimeBasedArchiveRemover, it seems that only the archived files are taken into account for calculating the total size, not the current log file.
So when using compressed archive files, the above configuration seems to be making sense after all. For example: as long as the total size of all compressed archive files is smaller than 100MB, there is no need to delete archive files, regardless of the size of the current log file (which could be greater than 100MB).
If I have to specify at least the same value for totalSizeCap as for maxFileSize (say 200MB), then compressed archive files will only be removed when their total size reaches 200MB, irrespective of the size of the current log file. So I might end up with the current log file and 199MB worth of compressed archive files, whereas I set out to cap the total amount of compressed log files at 100MB.