Alper Akture Alper Akture - 2 years ago 103
Java Question

RollingFileAppender that also deletes files older than some date

Do any of the popular Java logging frameworks support a rolling file appender, that I can configure to rollover daily, and also delete any log file that is over some number of days old? I know I could use a rolling file appender and a cron, but was wondering if anyone knew of an appender that can do both.

Answer Source

Logback's classic RollingFileAppender provides this and more. An example configuration from the manual (

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->

      <!-- keep 30 days' worth of history -->

      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>

  <root level="DEBUG">
    <appender-ref ref="FILE" />

This provides daily rollover and 30 days of history. Place this in a file called logback.xml, or logback-test.xml for test trees, and place it in the classpath.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download