Logback configuration daily rotate and zip monthly

故事扮演 提交于 2020-01-24 04:13:29

问题


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:


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.


来源:https://stackoverflow.com/questions/46520686/logback-configuration-daily-rotate-and-zip-monthly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!