Reputation: 61
I'm using the automatic log rolling and compression facilitated by the TimeBasedRollingPolicy provided in Log4J Extras (see config below).
It is normal for the application which is doing this logging to be constantly stopping/starting and I've noticed that the automatic compression does not occur if the application is stopped during a rollover triggering event (hourly rollover in this case). I find this strange as the rolling itself (without compression) still occurs and seems to work fine.
Is it not possible to have log compression work for an application that does not run continuously?
Does anyone know how to get this working with Log4J?
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true">
<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="/var/batchproc/logs/log4j_roll_compress_%d{yyyy-MM-dd-kk}.log.gz"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d] [%t] %-5p %c %m%n"/>
</layout>
</appender>
<root>
<appender-ref ref="ROLL"/>
</root>
</log4j:configuration>
Upvotes: 2
Views: 1320
Reputation: 1325
The rollover process is only triggered by logging two messages that are in different time units (hours in the example) while the app is running. Past time units aren't scanned-for on app startup.
One thing you can do is use a separate "active" file name to be where all log messages go before they're rolled/zipped. If you do that, any existing active log file will be appended-to until another hour has gone by and then rolled into a gzipped, timestamped file. Unfortunately, this file's timestamp isn't checked on startup (at least in apache-log4j-extras 1.1), so the old hour's logs and new hour's logs will be together in the rolled file. But at least it'll be zipped!
<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="File" value="/var/batchproc/logs/log4j_roll_active.log"/>
...rest of example config here...
</appender>
Upvotes: 2