I'm trying to manage my logging in a way in which my oldest archived logfiles are deleted once they've either reached the total cumulative size limit or reached their maximum history limit. When using the SizeAndTimeBasedRollingPolicy
in Logback 1.1.7, the rolling file appender will keep creating new archives in spite of exceeding the totalSizeCap
set.
Here's my logback.xml file for reference:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${USERPROFILE}/testlogs/test.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>
${USERPROFILE}/testlogs/%d{yyyy-MM-dd_HH}/test%i.log.zip
</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50KB</maxFileSize>
<totalSizeCap>200KB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p - %m%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="file" />
</root>
</configuration>
Is this a bug in logback or am I not configuring the rolling file appender correctly?
The lastest version of logback.qos.ch (1.1. 7) supports the property "totalSizeCap". This property can be used to limit the total size of archived log files. Currently, the top level nifi pom. xml specifies version 1.1.
Appenders are named entities. This ensures that they can be referenced by name, a quality confirmed to be instrumental in configuration scripts. The Appender interface extends the FilterAttachable interface. It follows that one or more filters can be attached to an appender instance.
Size-based rolling policy allows to rollover based on file on each log file. For example, we can rollover to a new file when the log file reaches 10 MB in size. The maxFileSize is used to specify the size of each file when it gets rolled over.
It's bug in Logback 1.1.7. See: http://jira.qos.ch/browse/LOGBACK-1166
I have checked, totalSizeCap
works in Logback 1.1.8-SNAPSHOT.
Well even the question is answered, I wanted to post the work around that we made until the bug is fixed in 1.1.8.
The bug 1166 simply does not apply totalSizeCap to the first two time units, depends on the smallest unit on the fileNamePattern you are using which means for your scenario it will not consider the logs of the first two hours for totalSize capping.
My configuration was somehow like so-taken from logback site examples-;
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- daily rollover --> <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- keep 30 days' worth of history capped at 3GB total size --> <maxHistory>30</maxHistory> <totalSizeCap>3GB</totalSizeCap> </rollingPolicy>
So we simply switched from {yyyy-MM-dd} to {yyyy-MM-dd_HH} and of course maximized the maxHistory to 30*24. Thus we made the last two hours not to be capped instead of last two days and for our case it was omittable. Of course, the log files will start to rollover in every hour but as I said it was ok for our unique case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With