QuarkusのログをJson形式で出力する

はじめに

ログコレクターでアプリの情報収集する場合、Jsonでログが出力されている方が何かとやりやすいことがありますがQuarkusでもログの出力をJsonに変えることができます。

ログをJsonに変更する

依存の追加

適当にQuarkusプロジェクトを作成し、以下の依存を追加します。

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-logging-json</artifactId>
</dependency>

Code Quarkusなどで最初から依存を追加しておくこともできますが、今回は1から自分で追加することを想定してやります。

というか、これだけでログはJsonに変更されます。

$  ./mvnw quarkus:dev
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< dev.hirooka:json-logging >----------------------
[INFO] Building json-logging 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- quarkus-maven-plugin:1.6.1.Final:dev (default-cli) @ json-logging ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] Nothing to compile - all classes are up to date
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
Listening for transport dt_socket at address: 5005
{"timestamp":"2020-08-02T23:57:27.733+09:00","sequence":1376,"loggerClassName":"org.jboss.logging.Logger","loggerName":"io.quarkus","level":"INFO","message":"json-logging 1.0-SNAPSHOT on JVM (powered by Quarkus 1.6.1.Final) started in 0.809s. Listening on: http://0.0.0.0:8080","threadName":"Quarkus Main Thread","threadId":47,"mdc":{},"ndc":"","hostName":"yuya-hirooka","processName":"json-logging-dev.jar","processId":23552}
{"timestamp":"2020-08-02T23:57:27.736+09:00","sequence":1377,"loggerClassName":"org.jboss.logging.Logger","loggerName":"io.quarkus","level":"INFO","message":"Profile dev activated. Live Coding activated.","threadName":"Quarkus Main Thread","threadId":47,"mdc":{},"ndc":"","hostName":"yuya-hirooka","processName":"json-logging-dev.jar","processId":23552}
{"timestamp":"2020-08-02T23:57:27.736+09:00","sequence":1378,"loggerClassName":"org.jboss.logging.Logger","loggerName":"io.quarkus","level":"INFO","message":"Installed features: [cdi, resteasy]","threadName":"Quarkus Main Thread","threadId":47,"mdc":{},"ndc":"","hostName":"yuya-hirooka","processName":"json-logging-dev.jar","processId":23552}

テスト時、開発時のログを通常のログに戻す

Jsonはログコレクター等のミドルウェアからの読み込みには都合が良いですが人間に撮って読みにくいことも多いと思います。その場合以下の設定をapplication.properties追加してやれば開発時とテスト時のログはもとに戻せます。

%dev.quarkus.log.console.json=false
%test.quarkus.log.console.json=false

./mvnw quarkus:devで開発モードで起動します。

$ ./mvnw quarkus:dev
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< dev.hirooka:json-logging >----------------------
[INFO] Building json-logging 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- quarkus-maven-plugin:1.6.1.Final:dev (default-cli) @ json-logging ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] Nothing to compile - all classes are up to date
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
Listening for transport dt_socket at address: 5005
__  ____  __  _____   ___  __ ____  ______ 
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ 
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \   
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/   
2020-08-03 00:03:51,991 INFO  [io.quarkus] (Quarkus Main Thread) json-logging 1.0-SNAPSHOT on JVM (powered by Quarkus 1.6.1.Final) started in 0.809s. Listening on: http://0.0.0.0:8080
2020-08-03 00:03:52,001 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2020-08-03 00:03:52,001 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy]

参考資料