Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compress Log4j files

Is it possible to compress log files (I doing it via RollingFileAppender )?

like image 428
EK. Avatar asked Jul 25 '10 13:07

EK.


People also ask

What is rolling file in Log4j?

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.

What is Appender rolling?

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.

How do you create a new log file for each time the application runs Log4j?

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.


1 Answers

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

like image 149
fasseg Avatar answered Sep 28 '22 03:09

fasseg