Logback configuration daily rotate and zip monthly

Viewed 3690

Is it possible to set logback's configuration to create a .log file every day and keep 30 files then zip the files in one zip and start to create .log again?

1 Answers

You can ...

create a .log file every day and keep 30 files

... using a RollingFileAppender with a TimeBasedRollingPolicy. Here's an example:

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- retain 30 days logs -->
      <maxHistory>30</maxHistory>
    </rollingPolicy>

    <encoder>
      <pattern>...</pattern>
    </encoder>
  </appender>

But there is no Logback appender which will then do this:

zip the files in one zip and start to create .log again

For that you could:

  • Write your own appender (plenty of details on this in the docs)

Or

  • Handle this outside Logback; you are dealing with files on a file system so a cron job which runs a shell script which finds all files from the last n days and zips them up and then deletes them would do the trick.
Related