Monday, 28 February 2022

Logback: print log messages in pretty json format

Using ‘'ch.qos.logback.contrib.json.classic.JsonLayout' layout we can log messages in json format.

<appender name="STDOUT"
	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>
		<appendLineSeparator>true</appendLineSeparator>
	</layout>
</appender>

 

I would recommend you, to not enable ‘prettyPrint’ in production to reduce space consumption.

 

Find the below working application.

 

Step 1: Create new maven project ‘logback-pretty-json’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>logback-pretty-json</artifactId>
	<version>1</version>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<dependencies>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.2.10</version>
		</dependency>


		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.2.10</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.36</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.13.1</version>
		</dependency>



	</dependencies>
</project>

 

Step 3: Create logback.xml file under src/main/resources folder.

 

logback.xml

<configuration scan="true" scanPeriod="200 seconds">

	<appender name="STDOUT"
		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>
			<appendLineSeparator>true</appendLineSeparator>
		</layout>
	</appender>


	<root level="INFO">
		<appender-ref ref="STDOUT" />
	</root>
</configuration>

 

Step 4: Define HelloWorld application class.

 

HelloWorld.java

 

package com.sample.app;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.core.joran.spi.JoranException;

public class HelloWorld {
	private static Logger logger = LoggerFactory.getLogger(HelloWorld.class);

	public static void main(String[] args) throws JoranException, IOException {

		logger.trace("Trace message");
		logger.debug("Debug message");
		logger.info("Info message");
		logger.warn("Warning message");
		logger.error("Error message");

	}
}

 

Total project structure looks like below.

 

 


Run HelloWorld application, you will see below messages in console.

{
  "timestamp" : "2022-02-25 12:43:50.799",
  "level" : "INFO",
  "thread" : "main",
  "logger" : "com.sample.app.HelloWorld",
  "message" : "Info message",
  "context" : "default"
}
{
  "timestamp" : "2022-02-25 12:43:50.813",
  "level" : "WARN",
  "thread" : "main",
  "logger" : "com.sample.app.HelloWorld",
  "message" : "Warning message",
  "context" : "default"
}
{
  "timestamp" : "2022-02-25 12:43:50.813",
  "level" : "ERROR",
  "thread" : "main",
  "logger" : "com.sample.app.HelloWorld",
  "message" : "Error message",
  "context" : "default"
}

 

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/logback/logback-pretty-json

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment