Logback RollingFileAppender maxHistory

时间:2016-03-04 08:25:42

标签: java logging logback

我有配置:

 <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/logs/logFile.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <pattern>${PATTERN}</pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

        <fileNamePattern>/logs/logFile.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>1MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
        <maxHistory>2</maxHistory>
    </rollingPolicy>
</appender>

但是,两天后日志不会被删除,可能是什么问题? 也许问题在于:

<fileNamePattern>/logs/logFile.%d{yyyy-MM-dd}.%i.log</fileNamePattern>

但我需要保存所有日志

1 个答案:

答案 0 :(得分:1)

请设置

<cleanHistoryOnStart>true</cleanHistoryOnStart>

并检查。默认情况下这是错误的。

https://logback.qos.ch/manual/appenders.html