简体   繁体   English

压缩 Log4j 文件

[英]Compress Log4j files

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

log4j extras has support for that. log4j extras对此有支持。 Just add the following to your RollingFileAppender configuration and have the filename end in .gz to automagically compress your log files:只需将以下内容添加到您的RollingFileAppender配置中,并将文件名以.gz结尾,以自动压缩您的日志文件:

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

For more details check the Javadoc有关更多详细信息,请查看Javadoc

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! RollingFileAppender - 我正在寻找同时具有 timeBased 和 sizeBased 滚动的东西,并且具有压缩......它两者都有! and I can drop my syslog4j library since this one has it also!我可以删除我的 syslog4j 库,因为这个库也有!

Please don't use log4j 1.2 + extras unless you really have to.请不要使用 log4j 1.2 + extras,除非你真的必须这样做。

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 周期来执行此操作,并且在服务器上我们有不超过一周的日志,在另一个文件服务器上我们有旧日志文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM