Is it possible to setup log4j to create gziped log files without creating intermediate .log files? There are several rolling strategies (rolling by date, etc), but they creates unzipped files first.
You can create you own Appender extending org.apache.log4j.RollingFileAppender and overriding the current logic by implementing your own optimized implementation version. Zip current file and discard rolling it to another log file (default implementation of RollingFileAppender). Example:
log4j.appender.{name}=br.com.sample.MyZipRollingFileAppender
You can "google" and search implementation examples using java.util.zip.ZipOutputStream or java.util.zip.GZIPOutputStream to zip the current file.
It is possible to solve this task using this Writer: http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/coyote/http11/filters/FlushableGZIPOutputStream.html and a code like
Writer writer = new OutputStreamWriter(new FlushableGZIPOutputStream(newFileOutputStream(logFileName), LINES_TO_FLUSH));
appender.setWriter(writer);
It works but there are some drawbacks: compression is lower and it is not always possible to unzip this file. So I returned back to rotation.
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