Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback configuration daily rotate and zip monthly

Tags:

logback

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?

like image 322
Antonio Mele Avatar asked Oct 18 '22 05:10

Antonio Mele


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.
like image 87
glytching Avatar answered Oct 21 '22 00:10

glytching