How to compress logs in WildFly

This article shows how to enable logs compression in WildFly by setting the appropriate suffix in your Periodic Rotating File Handler. In the second part of this tutorial we will learn how to compress logs using Log4j instead.

Compressing Logs natively with WildFly

Logs are an essential part of any software application, as they help us monitor and debug the system. However, logs can quickly become large and unwieldy, taking up valuable disk space and slowing down the system. That’s why compressing logs is a common practice in the industry.

Since WildFly 18 there is a straightforward way to enable logs compression by setting a suffix ending with .gz or .zip. For example, here is how to enable a daily log compressions of the Periodic Rotating File Handler on a daily base:

<periodic-rotating-file-handler name="FILE" autoflush="true">
	<formatter>
	    <named-formatter name="PATTERN"/>
	</formatter>
	<file relative-to="jboss.server.log.dir" path="server.log"/>
	<suffix value=".yyyy-MM-dd.zip"/>
	<append value="true"/>
</periodic-rotating-file-handler>

Clearly, the suffix determines how often the frequency of Logs compression. For example, to enable an hourly compression and rotation of logs you can use the following CLI command:

/subsystem=logging/periodic-rotating-file-handler=FILE:write-attribute(name=suffix,value=".yyyy-MM-dd-HH.zip")

You can check in your logs folder to see that now, after rotation, logs are compresses:

wildfly compress logs

Compressing Logs with Log4j

To compress logs with Log4j, we need to add a rolling policy to our log4j.xml configuration file. A rolling policy is responsible for rotating log files based on size or time. In this case, we will use the SizeAndTimeBasedRollingPolicy to compress logs based on both size and time.

The following is an example of how to add the SizeAndTimeBasedRollingPolicy to your log4j.xml configuration file:

<appender name="file" class="org.apache.log4j.rolling.RollingFileAppender">
  <rollingPolicy class="org.apache.log4j.rolling.SizeAndTimeBasedRollingPolicy">
    <!-- Compress logs once they reach 50 MB and keep up to 10 logs -->
    <param name="FileNamePattern" value="logs/application-%d{yyyy-MM-dd}-%i.log.gz"/>
    <param name="MaxFileSize" value="50MB"/>
    <param name="MaxBackupIndex" value="10"/>
  </rollingPolicy>
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
  </layout>
</appender>

In the above example, we defined a rolling policy that compresses logs once they reach 50 MB and keep up to 10 logs. The compressed logs will have a .gz file extension, and the log file name will include the date.

To learn more about configuring Log4j2 with WildFly, check this article: How to use Log4j2 in your WildFly applications

Conclusion

Compressing logs is an essential practice in software development, as it helps us manage disk space and optimize system performance. Log4j provides an easy and flexible way to compress logs, making it an excellent choice for Java-based applications.

Found the article helpful? if so please follow us on Socials