简体   繁体   English

Logback:SizeAndTimeBasedRollingPolicy不遵守totalSizeCap

[英]Logback: SizeAndTimeBasedRollingPolicy not honoring totalSizeCap

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. 在Logback 1.1.7中使用SizeAndTimeBasedRollingPolicy时,滚动文件追加器将继续创建新的存档,尽管超过了totalSizeCap集。

Here's my logback.xml file for reference: 这是我的logback.xml文件供参考:

<?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? 这是logback中的错误还是我没有正确配置滚动文件appender?

It's bug in Logback 1.1.7. 它是Logback 1.1.7中的错误。 See: http://jira.qos.ch/browse/LOGBACK-1166 请参阅: http//jira.qos.ch/browse/LOGBACK-1166

I have checked, totalSizeCap works in Logback 1.1.8-SNAPSHOT. 我已经检查过, totalSizeCap在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. 即使问题得到解答,我也想发布我们所做的工作,直到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. 错误1166根本不会将totalSizeCap应用于前两个时间单位 ,取决于您正在使用的fileNamePattern上的最小单位,这意味着对于您的方案,它不会考虑totalSize上限的前两个小时的日志。

My configuration was somehow like so-taken from logback site examples-; 我的配置在某种程度上就像从logback站点示例中获取的那样 - ;

 <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 . 所以我们只需从{yyyy-MM-dd}切换到{yyyy-MM-dd_HH} ,当然最大化maxHistory为30 * 24 Thus we made the last two hour s 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. 当然,日志文件将在每个小时开始翻转,但正如我所说,这对我们独特的情况来说是好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM