Log4j - compress log file in gz on the fly

Is it possible to configure log4j to create gziped log files without creating intermediate .log files? There are several riding strategies (rolling up by date, etc.), but first they create the unpacked files.

+4
source share
3 answers

You can solve this problem using this Writer: http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/coyote/http11/filters/FlushableGZIPOutputStream.html and code like

Writer writer = new OutputStreamWriter(new FlushableGZIPOutputStream(newFileOutputStream(logFileName), LINES_TO_FLUSH)); appender.setWriter(writer); 

This works, but there are some disadvantages: the compression is lower, and it is not always possible to unzip this file. Therefore, I returned to the rotation.

0
source

You can create your own Appender that extends org.apache.log4j.RollingFileAppender and overrides the current logic by implementing your own optimized version of the implementation. Replace the current file and drop it into another log file (standard implementation of RollingFileAppender). Example:

 log4j.appender.{name}=br.com.sample.MyZipRollingFileAppender 

You can google and look for implementation examples using java.util.zip.ZipOutputStream or java.util.zip.GZIPOutputStream to pin the current file.

+3
source

You can try org.apache.log4j.rolling.TimeBasedRollingPolicy:

 <appender...> <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy"> <param name="FileNamePattern" value="/wombat/log.%d{yyyy-MM}**.gz**"/> </rollingPolicy> </appender> 
0
source

Source: https://habr.com/ru/post/1482039/


All Articles