Is it possible to compress log files (I doing it via RollingFileAppender )?
Java Logging. Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.
RollingFileAppender is a file appender which rolls over the log files once it has reached a certain size limit or date/time pattern no longer applies. In this post, I will show you how to use the RollingFileAppender to backup and compress the old log files based on - Date and Time.
File with system properties name and some prefix text if you want. This will create a log file with current date time, something like this Log4jDemoApp-dd-MM-yyyy-hh-mm-ss. log every time we run the application. It will create the new file because for every run we are setting current date stamp in system properties.
log4j extras has support for that. Just add the following to your RollingFileAppender
configuration and have the filename end in .gz to automagically compress your log files:
<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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With