My answer is based on logback
, not log4j
(sorry for the confuse..)
You can achieve that log rotation by using TimeBasedRollingPolicy
.
for example)
<appender name="SYSTEMLOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./logs/system.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./logs/system.log.%d{yyyy-MM-dd}</fileNamePattern>
<!-- keep last 30 days of logs -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %msg %n</Pattern>
</encoder>
</appender>
It will rotate at midnight, and will delete log files older than 30 days.
fileNamePattern: The rollover period is inferred from the value of fileNamePattern
maxHistory: The optional maxHistory property controls the maximum number of archive files to keep, asynchronously deleting older files. For example, if you specify monthly rollover, and set maxHistory to 6, then 6 months worth of archives files will be kept with files older than 6 months deleted. Note as old archived log files are removed, any folders which were created for the purpose of log file archiving will be removed as appropriate.
You can check more information on TimeBasedRollingPolicy