Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j - DailyRollingFileAppender - Are rolled files deleted after some amount of time?

This answer implies that logs are kept for 7 days. Is this configurable?

This Java Ranch Post implies that files are never deleted.

This site also agrees that log files are never removed:

If you are trying to use the Apache Log4J DailyRollingFileAppender for a daily log file, you may need to want to specify the maximum number of files which should be kept. Just like rolling RollingFileAppender supports maxBackupIndex. But the current version of Log4j (Apache log4j 1.2.16) does not provide any mechanism to delete old log files if you are using DailyRollingFileAppender. I tried to make small modifications in the original version of DailyRollingFileAppender to add maxBackupIndex property. So, it would be possible to clean up old log files which may not be required for future usage.

Source

I can't find an authoritative answer and I don't want to wait 7 days to see if my logs are deleted.

like image 452
sixtyfootersdude Avatar asked Oct 22 '22 22:10

sixtyfootersdude


1 Answers

See this this post regarding Log4J deletes. In short it appears that dailyRollingFileAppender cannot. But perhaps you might want to look at switching to Logback. It was written by the same guy and can do what your looking for.

I use the following appender to maintain 30 days of HTML logs:

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>Logs\logFile.html</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.html</fileNamePattern>

      <!-- keep 30 days' worth of history -->
      <maxHistory>30</maxHistory>
    </rollingPolicy>

    <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
    <charset>UTF-8</charset>
        <layout class="ch.qos.logback.classic.html.HTMLLayout">
            <pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>
        </layout>           
    </encoder>
</appender>

The relevant sectionfrom the manual states the following on maxHistory:

The optional maxHistory property controls the maximum number of archive files to keep, 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.

Logback also has a Log4J properties translator available here to help with the transistion. As well as an entire chapter in their manual devoted to switching from Log4J.

like image 60
Robert H Avatar answered Oct 24 '22 10:10

Robert H