Configuring a Periodic Rotating File Handler

This article will teach you how to configure the Periodic File Handler in WildFly. We will also go through some common settings and advanced options such as compressing and rotating the logs file.

Handlers overview

A Log Handler receives a log events and exports them to a destination. Out of the box, WildFly ships with a Console Handler, which writes logs on the server console, and a Periodic Rotating File Handler, which writes logs to a File with a rotating policy.

Here is the default configuration for the Periodic Rotating File Handler:

<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"/>
    <append value="true"/>
</periodic-rotating-file-handler>

Here is a description of the attributes of the Handler:

  • append : set it to true to enable log file appending at server startup (default true).
  • autoflush : set it to true to allow automatic log flushing (default true).
  • suffix is a key element of the Periodic Handler configuration as it determines how often the file is rotated. The default yyyy-MM-dd rotates logs daily using a pattern type server.log.YYYY-MM-DD. If you want to change the time rotation policy, just choose a format understood by java.text.SimpleDateFormat; for example yyyy-MM-dd HH rotates the logs hourly.
  • path specifies where logs are written. If you include a relative-to directory, the path will be a relative path, otherwise (if relative-to is blank) it will be the absolute path where log files will be written.
  • level attribute defines the log level associated with the handler. Out of the box this handler logs all the events matching the verbosity level. If you want to apply some filters over the log events you can use the filter and filter-spec as indicated in the section “Filtering Logs”.
  • Finally, the formatter element provides support for formatting LogRecords. The log formatting inherits the same pattern strings for layout pattern of log4j, which was in turn inspired by old C’s printf function.

How to change the Log Path

Changing the attributes of the handlers is pretty simple as you can execute a write-attribute on the single element you want to change. Varying the log path, on the other hand, requires changing nested attributes of the file element.

Here is how to set the daily rolling appender to use the jboss.server.log.dir/node1.log path:

/subsystem=logging/periodic-rotating-file-handler=FILE/:write-attribute(name=file,value={"relative-to" => "jboss.server.log.dir","path" => "node1.log"})

The following command sets the log file path to the absolute path /tmp/server.log:

/subsystem=logging/periodic-rotating-file-handler=FILE/:write-attribute(name=file,value={"path" => "/tmp/node1.log"})

Compressing the Handler’s logs

You can configure the Periodic Rotating handler to compress your log files upon rotation. To do that, simply add “.zip” at the end of your suffix. Example:

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

Filtering the Logs

You can also decrease the log verbosity by using filters which you can apply on the log content. In order to do that, you can use a filter expression that is able to include/exclude log messages based on their text content.

For instance, here is how to filter out the text “IJ000906” from your logs with a CLI command:

/subsystem=logging/periodic-rotating-file-handler=FILE/:write-attribute(name=filter-spec,value=not(match("IJ000906")))

This will produce the following addition in your configuration:

<periodic-rotating-file-handler name="FILE" autoflush="true">
      <filter-spec value="not(match(&quot;IJ000906&quot;))"/>
</periodic-rotating-file-handler>

If, on the other hand, you were to choose to log any message containing either the text “JBAS” or “JBWS022052” then you could opt for the “any” filtering expression:

/subsystem=logging/periodic-rotating-file-handler=FILE/:write-attribute(name=filter-spec,value=any(match("JBAS"), match("JBWS022052"))))