Java 压缩 Log4j 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3329385/
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-13 22:13:31  来源:igfitidea点击:

Compress Log4j files

javapropertieslog4j

提问by EK.

Is it possible to compress log files (I doing it via RollingFileAppender )?

是否可以压缩日志文件(我通过 RollingFileAppender 压缩)?

采纳答案by fasseg

log4j extrashas support for that:

log4j extras支持:

just add the following to your RollingFileAppender configuration and having the filename end in .gzwill automagically compress your logfiles:

只需将以下内容添加到您的 RollingFileAppender 配置中,并以.gz结尾的文件名将自动压缩您的日志文件:

<appender...>
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
      <param name="FileNamePattern" value="/wombat/foo.%d{yyyy-MM}.gz"/>
    </rollingPolicy>
</appender>

check here for some details in the javadoc:

在此处查看 javadoc 中的一些详细信息:

http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html

http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html

hope that helped

希望有所帮助

回答by Romain Hippeau

I know this does not exactly answer your question, but it does propose an alternate solution.

我知道这并不能完全回答您的问题,但它确实提出了替代解决方案。

The way we handle that is by having a batch process run at the end of the day that compresses any prior log files to today's date, get's rid of any prior to a week old and then copies them off to another file server. This way the application does not need to consume any CPU cycles doing this and on the server we have logs that are no older than a week and on another file server we have older log files.

我们处理这个问题的方法是在一天结束时运行一个批处理程序,将任何先前的日志文件压缩到今天的日期,删除一周之前的任何日志文件,然后将它们复制到另一个文件服务器。这样,应用程序不需要消耗任何 CPU 周期来执行此操作,并且在服务器上我们有不超过一周的日志,在另一个文件服务器上我们有旧日志文件。

回答by Nikhil Mulley

The only other thing I see is upgrade log4j to more than 1.3.15/1.4 and use TimeBasedRollingPolicy roller to have automatic compression enabled, but that would require an upgrade of log4j essentially (and compatibility of the app as well)

我看到的另一件事是将 log4j 升级到 1.3.15/1.4 以上并使用 TimeBasedRollingPolicy 滚轮启用自动压缩,但这基本上需要升级 log4j(以及应用程序的兼容性)

Hmmm..may be another better thing to have is let have log4j execute a command once the file is rolled. So instead of gzip, I can use some other compression logic like bzip, or lzop etc.. that would be another approach if I have this facility in log4j today.

嗯..可能是另一件更好的事情是让 log4j 在文件滚动后执行命令。因此,我可以使用其他一些压缩逻辑,如 bzip 或 lzop 等,而不是 gzip。如果我今天在 log4j 中有此功能,那将是另一种方法。

回答by Daren

Try Log4j v2.x

尝试 Log4j v2.x

I came upon this great answer, but then kept investigating a bit and log4j 2 is out!

我找到了这个很好的答案,但后来继续调查了一下,log4j 2 出来了!

Now you can have compression with the basic library and much, much more... it's just awesome!

现在您可以使用基本库进行压缩,还有更多……真是太棒了!

RollingFileAppender- I was looking for something that had both timeBased and sizeBased rolling, and with compression... it has both! and I can drop my syslog4j library since this one has it also!

RollingFileAppender- 我正在寻找同时具有 timeBased 和 sizeBased 滚动的东西,并且具有压缩......它两者都有!我可以删除我的 syslog4j 库,因为这个库也有!

Please don't use log4j 1.2 + extras unless you really have to.

请不要使用 log4j 1.2 + extras,除非你真的必须这样做。