的logback。仅存储当天的日志

时间:2017-06-22 08:54:36

标签: java logging logback appender rollingfileappender

我在项目中使用了logback。我有问题只存储当天的日志。 我的logback appender配置:

<appender name="appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>log/log.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>log/log.%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>512MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
        <maxHistory>1</maxHistory>
    </rollingPolicy>
    <encoder>
        <charset>utf-8</charset>
        <pattern>%d{dd-MM HH:mm:ss.SSS} [%thread] %-5level %logger{66} - %msg%n</pattern>
    </encoder>
</appender>

我想查看文件log.2017-06.22.0.zip,log.2017-06.22.1.zip ..如果当前日期是2017-06-22。必须删除所有以前的日志。如果我将maxHistory设置为1,我会看到当前日志和前一天日志。请帮帮我。

1 个答案:

答案 0 :(得分:0)

不幸的是,对于当前版本的logback(1.2.3),我不认为这是可能的。检查ch.qos.logback.core.rolling.helper.TimeBasedArchiveRemover的源代码,看看如何使用参数maxHistory:

// below is line 97
void capTotalSize(Date now) {
    long totalSize = 0;
    long totalRemoved = 0;
    for (int offset = 0; offset < maxHistory; offset++) {
        // snip : execute removal
        // notice that if you set maxHistory to 0 or -1 the loop contents will not be executed at all.
    }
    addInfo("Removed  " + new FileSize(totalRemoved) + " of files");
}

您可能希望向回溯项目提交功能请求(或PR)。