Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j - compress log file to gz on fly

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.

like image 357
dbf Avatar asked May 21 '13 20:05

dbf


2 Answers

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.

like image 109
Michael Henrique Avatar answered Oct 06 '22 20:10

Michael Henrique


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.

like image 29
dbf Avatar answered Oct 06 '22 19:10

dbf