简体   繁体   English

文件大小超过2MB后可以log4j压缩日志文件

[英]Can log4j compress a log file after file size pass 2MB

What I want is this: Just like Glassfish 3 server log, if the log file pass certain file size(like 2MB), the log file will automatically zip itself, so that the log file will not become too large. 我想要的是:就像Glassfish 3服务器日志一样,如果日志文件通过特定的文件大小(例如2MB),则日志文件将自动压缩自身,以使日志文件不会变得太大。 I am not sure if this is done via log api or cron api. 我不确定这是通过log api还是cron api完成的。 Is it possible to obtain what I describe above using log4j or is there some type of cron process involve here? 是否可以使用log4j获得我上面描述的内容,或者这里涉及某种cron进程? Here is my property file for log4j 这是我的log4j属性文件

log4j.rootLogger=DEBUG, LOG , stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d{d/MM/yy HH:mm:ss}:%m%n
# log4j.appender.LOG.Threshold=INFO

log4j.appender.LOG=org.apache.log4j.RollingFileAppender
log4j.appender.LOG.File=C:\\dev\\harry\\data\\logs\\core.log
log4j.appender.LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG.Append=true
log4j.appender.LOG.layout.ConversionPattern=%5p %d{d/MM/yy HH:mm:ss}:%m%n
# log4j.appender.LOG.Threshold=INFO

In the log4j extras project log4j extras project there are many useful appenders that can be used. 在log4j extras项目log4j extras项目中 ,有许多有用的附加程序可以使用。 In particular the with the RollingFileAppender configured with the TimeBasedRollingPolicy can automatically compress the rolled file. 特别是在使用TimeBasedRollingPolicy配置的RollingFileAppender的情况下 ,可以自动压缩滚动文件。 I don't know if the SizeBasedTriggeringPolicy does the same, and the documentation is scarse. 我不知道SizeBasedTriggeringPolicy是否做同样的事情 ,并且文档很匮乏。

I think that it would be easy to build a custom RollingPolicy that is SizeBased and compress the output, given that all the ingredients are present in the Log4j extras project (even the compression of the file). 我认为,鉴于Log4j extras项目中存在所有成分(甚至文件的压缩),构建一个基于SizeBased的自定义RollingPolicy并压缩输出也很容易。 It is basically the same code as the TimeBased policy, implementing the isTriggeringEvent method to be the same as the SizeBased policy. 它与TimeBased策略基本上是相同的代码,实现isTriggeringEvent方法与SizeBased策略相同。

Or something like that. 或类似的东西。

Basically to overcome the size limit of log files, we used to create appenders log files if size limit increase beyond max size. 基本上是为了克服日志文件的大小限制,如果大小限制超出最大大小,我们通常会创建追加程序日志文件。 Please check below article Rolling file appenders 请检查以下文章滚动文件附录

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

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