Thang Pham
Thang Pham

Reputation: 38705

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. I am not sure if this is done via log api or cron api. Is it possible to obtain what I describe above using log4j or is there some type of cron process involve here? Here is my property file for 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

Upvotes: 0

Views: 3568

Answers (2)

developer
developer

Reputation: 9478

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 articleRolling file appenders

Upvotes: 1

Soronthar
Soronthar

Reputation: 1601

In the log4j extras projectlog4j extras project there are many useful appenders that can be used. In particular the with the RollingFileAppender configured with the TimeBasedRollingPolicy can automatically compress the rolled file. I don't know if the SizeBasedTriggeringPolicy does the same, and the documentation is scarse.

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). It is basically the same code as the TimeBased policy, implementing the isTriggeringEvent method to be the same as the SizeBased policy.

Or something like that.

Upvotes: 2

Related Questions