Create ndjson logging for spring boot applications

Share on:

Logging ndjson to a file

We will configure spring-boot application to redirect logs to a file in ndjson format. See more information on ndjson

Dependencies

Add following dependencies to the pom.xml

 1 <dependency>
 2    <groupId>ch.qos.logback.contrib</groupId>
 3    <artifactId>logback-json-classic</artifactId>
 4    <version>0.1.5</version>
 5</dependency>
 6<dependency>
 7    <groupId>ch.qos.logback.contrib</groupId>
 8    <artifactId>logback-jackson</artifactId>
 9    <version>0.1.5</version>
10</dependency>

For update version please check here

Update logback.xml

 1<?xml version="1.0" encoding="UTF-8"?>
 2<configuration>
 3    <timestamp key="time" datePattern="yyyy-MM-dd HH:mm:ss,SSS"/>
 4    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
 5        <encoder>
 6            <pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n
 7            </pattern>
 8        </encoder>
 9    </appender>
10
11    <root level="info">
12        <appender-ref ref="STDOUT"/>
13    </root>
14
15    <appender name="NDJSON_FILE" class="ch.qos.logback.core.FileAppender">
16        <file>app-logs.txt</file>
17        <append>true</append>
18        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
19            <pattern>%msg%n</pattern>
20        </encoder>
21        <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
22            <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
23                <prettyPrint>false</prettyPrint>
24            </jsonFormatter>
25            <appendLineSeparator>true</appendLineSeparator>
26            <timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
27            <includeContextName>false</includeContextName>
28        </layout>
29    </appender>
30
31    <logger name="in.silentsudo.samplelog" level="all">
32        <appender-ref ref="NDJSON_FILE"/>
33    </logger>
34
35</configuration>

Note for logger name=in.silentsudo.samplelog the appender is NDJSON_FILE. After execuring this in spring boot application you should see file present at the project/$ROOT/app-logs.txt location. Content of the files can be given below:

1{"timestamp":"2022-01-26 16:26:00.323","level":"INFO","thread":"restartedMain","logger":"in.silentsudo.samplelog.samplelogApplication","message":"Starting samplelogA pplication using Java 16.0.1 on ashish with PID 12683 (/home/ashish/apps/web_apps/samplelog/target/classes started by ashish in /home/ashish/apps/web_apps/sprint-boot-rest-template)"}
2{"timestamp":"2022-01-26 16:26:00.332","level":"DEBUG","thread":"restartedMain","logger":"in.silentsudo.samplelog.samplelogApplication","message":"Running with Spring Boot v2.6.3, Spring v5.3.15"}
3{"timestamp":"2022-01-26 16:26:00.333","level":"INFO","thread":"restartedMain","logger":"in.silentsudo.samplelog.samplelogApplication","message":"No active profile set, falling back to default profiles: default"}
4{"timestamp":"2022-01-26 16:26:01.051","level":"INFO","thread":"restartedMain","logger":"in.silentsudo.samplelog.samplelogApplication","message":"Started samplelogA pplication in 0.919 seconds (JVM running for 1.432)"}

🗒️ Happy logging.

comments powered by Disqus