每日回滚配置,每月旋转和压缩 [英] Logback configuration daily rotate and zip monthly

查看:92
本文介绍了每日回滚配置,每月旋转和压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以设置logback的配置以每天创建一个.log文件并保存30个文件,然后将这些文件压缩为一个zip文件,然后再次开始创建.log?

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?

推荐答案

您可以...

每天创建一个.log文件,并保存30个文件

create a .log file every day and keep 30 files

...将RollingFileAppenderTimeBasedRollingPolicy一起使用.这是一个示例:

... 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>

但是没有Logback附加程序可以做到这一点:

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

将文件压缩为一个zip并再次开始创建.log

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

为此,您可以:

  • 在Logback之外处理此问题;您正在处理文件系统上的文件,因此执行cron作业(运行shell脚本)可以找到最近 n 天的所有文件,然后将其压缩然后删除,就可以解决问题.
  • 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.

这篇关于每日回滚配置,每月旋转和压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆