😸

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

2022/01/02に公開

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

やり方はSpring Bootと同じでlogstash-logback-encoderを使用する。
https://zenn.dev/kentama/articles/4fde9b73cccc605a7f92

環境

  • Micronaut: 3.2.3
  • Java: 17

準備

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

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

src/main/resources のlogback.xmlを修正する。

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

確認

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

 __  __ _                                  _   
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  Micronaut (v3.2.3)

{"@timestamp":"2022-01-02T17:00:21.659+09:00","@version":"1","message":"No optimizations class io.micronaut.core.reflect.ClassUtils$Optimizations found","logger_name":"io.micronaut.core.optim.StaticOptimizations","thread_name":"main","level":"DEBUG","level_value":10000}
{"@timestamp":"2022-01-02T17:00:21.663+09:00","@version":"1","message":"No optimizations class io.micronaut.core.util.EnvironmentProperties found","logger_name":"io.micronaut.core.optim.StaticOptimizations","thread_name":"main","level":"DEBUG","level_value":10000}
...
{"@timestamp":"2022-01-02T17:00:21.959+09:00","@version":"1","message":"Found no matching beans of type [ApplicationEventListener] for qualifier: <ServiceReadyEvent> ","logger_name":"io.micronaut.context.DefaultBeanContext","thread_name":"main","level":"DEBUG","level_value":10000}
{"@timestamp":"2022-01-02T17:00:21.96+09:00","@version":"1","message":"Startup completed in 330ms. Server Running: http://localhost:8080","logger_name":"io.micronaut.runtime.Micronaut","thread_name":"main","level":"INFO","level_value":20000}

Discussion