Java 日志4j。每小时滚动,每天拉链

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18130836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:37:00  来源:igfitidea点击:

log4j. Rollover each hour, zip daily

javalogginglog4j

提问by Alec

Can I configure log4j to rollover each hour, then compress all the daily log-files into one zip (so that zip contains 24 log-files).

我可以将 log4j 配置为每小时翻转一次,然后将所有日常日志文件压缩到一个 zip 文件中(以便 zip 文件包含 24 个日志文件)。

Ideally I'd like to zip files only for those days which are one week old and earlier. But that's another part of the question.

理想情况下,我只想为一周前和更早的那些日子压缩文件。但这是问题的另一部分。

回答by Khinsu

You probably want to use a DailyRollingFileAppender. To roll them hourly, for example, you'd use a DatePattern of '.'yyyy-MM-dd-HH. For a log4j.properties file:

您可能想要使用DailyRollingFileAppender。例如,要每小时滚动一次,您可以使用 DatePattern '.'yyyy-MM-dd-HH。对于 log4j.properties 文件:

log4j.appender.myAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myAppender.DatePattern='.'yyyy-MM-dd-HH
...

Or for your programmatic configuration:

或者对于您的编程配置:

DailyRollingFileAppender appender = new DailyRollingFileAppender();
appender.setDatePattern("'.'yyyy-MM-dd-HH");

Logger root = Logger.getRootLogger();
root.addAppender(appender);

Unfortunately, using a DailyRollingFileAppender means that you can't limit the file size - this could be problematic if you have tons of logs in the given rolled period.

不幸的是,使用 DailyRollingFileAppender 意味着您无法限制文件大小 - 如果您在给定的滚动期内有大量日志,这可能会出现问题。

To compress take a look to: compress-log4j-files

要压缩,请查看:compress-log4j-files