Spring Boot의 응용 로그 JSON화

8959 단어 Springtech
Spring Boot의 응용 로그 JSON을 작성합니다.
JSON화를 위해 logstash-logback-encorder를 사용합니다.
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>
    

    확인


    여기서 스프링부트를 시작하면 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
    }
    

    좋은 웹페이지 즐겨찾기