JSON Logging for Spring Boot After that, the contents implemented according to the above article are posted. The above article has more information, so if you are not comfortable with English, we recommend that you refer to it.
Added logstash to dependencies
build.gradle
dependencies {
implementation 'net.logstash.logback:logstash-logback-encoder:6.1'
}
Added logback settings
src/main/resources/logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property resource="application.yml" />
<contextName>${spring.application.name}</contextName>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<fieldName>timeStamp</fieldName>
<timeZone>UTC</timeZone>
</timestamp>
<loggerName>
<fieldName>logger</fieldName>
</loggerName>
<logLevel>
<fieldName>level</fieldName>
</logLevel>
<threadName>
<fieldName>thread</fieldName>
</threadName>
<mdc />
<message>
<fieldName>message</fieldName>
</message>
<arguments>
<includeNonStructuredArguments>false</includeNonStructuredArguments>
</arguments>
<stackTrace>
<fieldName>stack</fieldName>
</stackTrace>
</providers>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
Example of outputting INFO log in Controller class
TestController.java
package com.exsample.controller;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static net.logstash.logback.argument.StructuredArguments.v;
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping
public void get() {
MDC.put("MDC key","MDC value");
log.info("JSON Log",v("arg key","arg value"));
}
}
The following log is output
{"timeStamp":"2019-08-28T08:52:22.173Z","logger":"com.exsample.controller.TestController","level":"INFO","thread":"main","MDC key":"MDC value","message":"JSON Log","arg key":"arg value"}
<encoder>
tag of logback-spring.xml
are output in order from the top.(Excerpt) logback-spring.xml
<providers>
<timestamp>
<fieldName>timeStamp</fieldName>
<timeZone>UTC</timeZone>
</timestamp>
<loggerName>
<fieldName>logger</fieldName>
</loggerName>
<logLevel>
<fieldName>level</fieldName>
</logLevel>
<threadName>
<fieldName>thread</fieldName>
</threadName>
<mdc />
<message>
<fieldName>message</fieldName>
</message>
<arguments>
<includeNonStructuredArguments>false</includeNonStructuredArguments>
</arguments>
<stackTrace>
<fieldName>stack</fieldName>
</stackTrace>
</providers>
Log contents
{
"timeStamp": "2019-08-28T08:52:22.173Z",
"logger": "com.exsample.controller.TestController",
"level": "INFO",
"thread": "main",
"MDC key": "MDC value",
"message": "JSON Log",
"arg key": "arg value"
}
MDC.put ()
are output at the position of the <mdc />
tag.(Excerpt) TestController.java
MDC.put("MDC key","MDC value");
Log excerpt
"MDC key": "MDC value"
log.info ()
is output at the position of the <message>
tag.(Excerpt) TestController.java
log.info("JSON Log",v("arg key","arg value"));
Log excerpt
"message": "JSON Log"
StructuredArguments.v (key, value)
after the second argument of log.info ()
, the contents will be output at the position of the <arguments>
tag.(Excerpt) TestController.java
log.info("JSON Log",v("arg key","arg value"));
Log excerpt
"arg key": "arg value"
Recommended Posts