😎

Spring BootのアプリケーションログをJSON化する

3 min read

Spring BootのアプリケーションログをJSON化する。
JSON化にはlogstash-logback-encoderを使用する。

https://github.com/logstash/logstash-logback-encoder

環境

  • Spring Boot: 2.4.5
  • Gradle: 6.8.3
  • Java: 11.0.6

準備

Gradleの依存関係に以下を追加する

dependencies {
    // ...
    implementation("net.logstash.logback:logstash-logback-encoder:6.6")
}

src/main/resources にlogback-spring.xmlを作成する。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
    </appender>

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

確認

ここでSpring Bootを起動するとJSON化されていることが確認できる。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.5)

{"@timestamp":"2021-06-19T15:04:16.404+09:00","@version":"1","message":"Starting JsonLogApplicationKt using Java 11.0.6 on local with PID 12390 (xxxxx)","logger_name":"com.example.jsonlog.JsonLogApplicationKt","thread_name":"main","level":"INFO","level_value":20000}
{"@timestamp":"2021-06-19T15:04:16.409+09:00","@version":"1","message":"No active profile set, falling back to default profiles: default","logger_name":"com.example.jsonlog.JsonLogApplicationKt","thread_name":"main","level":"INFO","level_value":20000}
{"@timestamp":"2021-06-19T15:04:16.726+09:00","@version":"1","message":"Started JsonLogApplicationKt in 0.641 seconds (JVM running for 1.333)","logger_name":"com.example.jsonlog.JsonLogApplicationKt","thread_name":"main","level":"INFO","level_value":20000}

カスタマイズ

読みやすくする

読みやすく表示することができる。
logback-spring.xmlを以下のように書き換える。

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
-    <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
+    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
+        <jsonGeneratorDecorator class="net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator"/>
+    </encoder>
</appender>
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.5)

{
  "@timestamp" : "2021-06-19T15:55:47.822+09:00",
  "@version" : "1",
  "message" : "Starting JsonLogApplicationKt using Java 15.0.2 on km.local with PID 6536 (xxxxx)",
  "logger_name" : "com.example.jsonlog.JsonLogApplicationKt",
  "thread_name" : "main",
  "level" : "INFO",
  "level_value" : 20000
}
{
  "@timestamp" : "2021-06-19T15:55:47.827+09:00",
  "@version" : "1",
  "message" : "No active profile set, falling back to default profiles: default",
  "logger_name" : "com.example.jsonlog.JsonLogApplicationKt",
  "thread_name" : "main",
  "level" : "INFO",
  "level_value" : 20000
}
{
  "@timestamp" : "2021-06-19T15:55:48.216+09:00",
  "@version" : "1",
  "message" : "Started JsonLogApplicationKt in 0.925 seconds (JVM running for 1.237)",
  "logger_name" : "com.example.jsonlog.JsonLogApplicationKt",
  "thread_name" : "main",
  "level" : "INFO",
  "level_value" : 20000
}

Discussion

ログインするとコメントできます