java-log-json-output
Get Log Output in JSON
1. Introduction
Most Java logging libraries today offer different layout options for formatting logs – to accurately fit the needs of each project.
In this quick article, we want to format and output our log entries as JSON. We’ll see how to do this for the two most widely used logging libraries: Log4j2 and Logback.
Both use Jackson internally for representing logs in the JSON format.
For an introduction to these libraries take a look at our introduction to Java Logging article.
2. Log4j2
Log4j2 is the direct successor of the most popular logging library for Java, Log4J.
As it’s the new standard for Java projects, we’ll show how to configure it to output JSON.
2.1. Dependencies
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
</dependencies>
The latest versions of the previous dependencies can be found on Maven Central: log4j-api, log4j-core, jackson-databind.
2.2. Configuration
Then, in our log4j2.xml file, we can create a new Appender that uses JsonLayout and a new Logger that uses this Appender:
<Appenders>
<Console name="ConsoleJSONAppender" target="SYSTEM_OUT">
<JsonLayout complete="false" compact="false">
<KeyValuePair key="myCustomField" value="myCustomValue" />
</JsonLayout>
</Console>
</Appenders>
<Logger name="CONSOLE_JSON_APPENDER" level="TRACE" additivity="false">
<AppenderRef ref="ConsoleJSONAppender" />
</Logger>
As we can see in the example config, it’s possible to add our own values to the log using KeyValuePair, that even supports lookout into the log context.
Setting the compact parameter to false will increase the size of the output but will make it also more human readable.
2.3. Using Log4j2
Logger logger = LogManager.getLogger("CONSOLE_JSON_APPENDER");
logger.debug("Debug message");
The debug output message for the previous code would be:
{
"timeMillis" : 1513290111664,
"thread" : "main",
"level" : "DEBUG",
"loggerName" : "CONSOLE_JSON_APPENDER",
"message" : "My debug message",
"endOfBatch" : false,
"loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"threadId" : 1,
"threadPriority" : 5,
"myCustomField" : "myCustomValue"
}
3. Logback
Logback can be considered another successor of Log4J. It’s written by same developers and claims to be more efficient and faster than its predecessor.
So, let’s see how to configure it to get the output of the logs in JSON format.
3.1. Dependencies
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-json-classic</artifactId>
<version>0.1.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-jackson</artifactId>
<version>0.1.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
</dependencies>
We can check here for the latest versions of these dependencies: logback-classic, logback-json-classic, logback-jackson, jackson-databind
3.2. Configuration
After that, we can create a new logger that uses this appender:
<appender name="json" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<jsonFormatter
class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>true</prettyPrint>
</jsonFormatter>
<timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
</layout>
</appender>
<logger name="jsonLogger" level="TRACE">
<appender-ref ref="json" />
</logger>
As we see, the parameter prettyPrint is enabled to obtain a human-readable JSON.
3.3. Using Logback
Logger logger = LoggerFactory.getLogger("jsonLogger");
logger.debug("Debug message");
With this – we’ll obtain the following output:
{
"timestamp" : "2017-12-14 23:36:22.305",
"level" : "DEBUG",
"thread" : "main",
"logger" : "jsonLogger",
"message" : "Debug log message",
"context" : "default"
}