How to print logs in JSON format in WildFly

This article discusses the configuration you can apply in WildFly to enable logging in JSON format. We will learn how to do that natively in WildFly or using a Log4j custom configuration.

Strategies for logging in JSON

There are several use cases in which logging in JSON format can be useful. For example:

Use cases for printing logs in JSON format

  1. Centralized logging: You can send JSON format logs to a centralized log management tool like Logstash, which can index and search logs for quick analysis.
  2. Machine-readable logs: Most tools allow parsing JSON texts and automate log analysis and error reporting.

On the other hand, you should consider that JSON logs are quite verbose and generally less human-readable. Therefore you should evaluate pros and cons of each aspect.

After this premise, let’s see how to enable JSON logs in WildFly. You can do that using the CLI, by running the following command:

/subsystem=logging/json-formatter=JSON:add(pretty-print=true, exception-output-type=formatted)
/subsystem=logging/periodic-rotating-file-handler=FILE-JSON:add(file={"relative-to"=>"jboss.server.log.dir", path=server.json}, suffix=".yyyy-MM-dd", append=true, autoflush=true, named-formatter=JSON)
/subsystem=logging/root-logger=ROOT:add-handler(name=FILE-JSON)

As you can see from the logs folder, now you have an additional server.json file:

wildfly logs json format

If you check this file, you will see that it contains arrays of JSON logs in it. For example:

{
    "timestamp": "2023-02-06T10:18:12.495+01:00",
    "sequence": 170,
    "loggerClassName": "org.jboss.as.server.logging.ServerLogger_$logger",
    "loggerName": "org.jboss.as",
    "level": "INFO",
    "message": "WFLYSRV0051: Admin console listening on http://127.0.0.1:9990",
    "threadName": "Controller Boot Thread",
    "threadId": 285,
    "mdc": {
    },
    "ndc": "",
    "hostName": "fedora",
    "processName": "jboss-modules.jar",
    "processId": 17766
}

How to print logs in JSON format with log4j

To print logs in JSON format, you will need to use a log4j appender that supports this format. One such appender is the log4j-jsonevent-layout library. Here’s how to set it up in your log4j configuration file:

Firstly, add the log4j-jsonevent-layout library to your project’s classpath. You can do this by adding the following dependency to your build file:

<dependency>
   <groupId>com.lmax</groupId>
   <artifactId>log4j-jsonevent-layout</artifactId>
   <version>1.7</version>
</dependency>

Then, in your log4j configuration file, define a new appender that uses the JSONEventLayout.

log4j.appender.json=org.apache.log4j.RollingFileAppender
log4j.appender.json.layout=com.lmax.log4j.jsonevent.JSONEventLayoutV1
log4j.appender.json.File=logs/application.json
log4j.appender.json.MaxFileSize=10MB
log4j.appender.json.MaxBackupIndex=10

Finally, assign the newly defined appender to a logger in your configuration file.

log4j.rootLogger=DEBUG, json

That’s it! Now, whenever you log messages using log4j, they will be written in JSON format to the specified file.

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

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