Print Logs Output as JSON String in Spring Boot Java — Log4j2
Assumptions:
You already have spring boot project. If you don’t, take the tutorial — https://spring.io/guides/gs/spring-boot/
If you have spring boot project, your pom.xml would be something like the following:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.vb.math</groupId>
<artifactId>learnit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>learnit</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring boot comes default packaged with logging. Refer here — https://docs.spring.io/spring-boot/docs/2.1.x/reference/html/boot-features-logging.html
The format of the default log is:
INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms
2019-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean
You want JSON log out put.
We will achieve that using log4j2.
Step 1:
Include the log4j2 spring boot dependency and exclude the any version conflicts of including common libs at multiple places.
<!-- New Dependencies and Exclusions --><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.3</version>
</dependency><!-- Exclusions in case you need --><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
Step 2:
If you have been using any other logs please check and remove those also using exclusions as above. Check dependencies using the following and remove them:
mvn dependency:tree -Dincludes=*log*
Step 3:
Create a file with name log4j2-spring.xml with the following content and add to your resources folder.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<JsonLayout complete="false" compact="true" eventEol="true"></JsonLayout>
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
</Console>
</Appenders>
<Loggers>
<!-- LOG everything at INFO level -->
<Root level="info">
<AppenderRef ref="Console" />
</Root>
<Logger name="com.vb" level="trace"></Logger>
</Loggers>
</Configuration>
You have to replace the italics. These are the pattern of the log and your packages where you want your logger to be available.
Step 4:
Use your logger.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// Instantiate your logger
private static Logger logger - LoggerFactory.getLogger(MyClassName.class);//Print the logslogger.debug(" Debug Message");
logger.error(" Error Message");
The output would be something like the following:
{
"thread": "main",
"level": "INFO",
"loggerName": "com.vb.math.learnit.LearnitApplication",
"message": "Started LearnitApplication in 2.508 seconds (JVM running for 4.586)",
"endOfBatch": false,
"loggerFqcn": "org.apache.commons.logging.LogAdapter$Log4jLog",
"instant": {
"epochSecond": 1597070439,
"nanoOfSecond": 853000000
},
"threadId": 1,
"threadPriority": 5
}