My logback.xml file as following, and I set MaxHistory=1 in order to delete old log files and only keep the log for one day. But I found the old files didn't get removed. I still could see them as:app.log.2019-02-11 app.log.2019-02-12
<configuration>
<property name="APP_NAME" value="logbacktest-logs" />
<property name="LOG_HOME" value="/tmp/${APP_NAME}" />
<property name="ENCODER_PATTERN" value="%d %C.%method:%L _ %msg%n"/>
<contextName>${APP_NAME}</contextName>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${ENCODER_PATTERN}</pattern>
</encoder>
</appender>
<appender name="APP_APPEND" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/app.log.%d{yyyy-MM-dd}</fileNamePattern>
<MaxHistory>1</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>${ENCODER_PATTERN}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="APP_APPEND" />
</root>
</configuration>
This is a part of my log
12:11:32,358 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
which means that say your application is not up during that time, then it doesn't get a chance to delete it
I had faced a same issue when I had set the maxHistory to 5, it deleted 5 log files sometimes but when my application died it could not delete the logs because it didn't get a chance to delete.
Refering the logback docs, it says you can use this:
<cleanHistoryOnStart> true </cleanHistoryOnStart>
If set to true, archive removal will be executed on appender start up. By default this property is set to false.
Archive removal is normally performed during roll over. However, some applications may not live long enough for roll over to be triggered. It follows that for such short-lived applications archive removal may never get a chance to execute. By setting cleanHistoryOnStart to true, archive removal is performed at appender start up.
Modify your piece of logback-spring.xml to:
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/app.log.%d{yyyy-MM-dd}</fileNamePattern>
<maxHistory>1</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>