Search code examples
javalog4jgziplog4j2

How can I write a gzip byte array to disk with log4j2?


I have a string that I want to log with log4j2. I first want to compress that string then write the resulting bytes to the log file. So the code looks something like this

ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(baos);
gzip.write(myLogString.getBytes());
gzip.close();
log.info(baos.toByteArray());

Unfortunately it looks like log4j at some point converts the byte array to a string which results in a file that's not in GZIP format. Is there any way to accomplish what I'm after?


Solution

  • You are approaching the problem from the wrong side: aspects such as compression and file output should be dealt with by the logging backend, not at the level of logging API.

    The feature of directly writing to compressed log files have been requested a couple of times, most recently in LOG4J2-3639, but was never implemented. It has some inherent problems like the risk of corrupted log files upon application crash.

    However, due to the modular nature of Log4j2, you can write your own custom component base on FileAppender/FileManager that wraps the output stream in a GZIPOutputStream.

    Edit: Alternative solutions might be:

    • using a size base rolling file appender with a small size limit. The current log file will be uncompressed, but it will rotate often,
    • since a concatenation of GZIP files is a valid GZIP file, you can buffer log messages in memory and write them to your file, when the buffer is full.