1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【SpringBoot】tomcatのアクセスログをJSON形式で出力する

Posted at

目的

logback-accessを使用してtomcatのアクセスログをJSON形式で出力する方法をまとめました。

HowTo

1. build.gradleのdependenciesに以下を追加

JSONへの変換はlogstash-logback-encoderを使用します。
logback-accessの設定はlogback-access-spring-boot-starterを入れるだけです。


  implementation("net.logstash.logback:logstash-logback-encoder:6.6")
  implementation("net.rakugakibox.spring.boot:logback-access-spring-boot-starter:2.7.1")

2. resources/にlogback-access.xmlを追加

用意されたフィールドを出力する場合

logback-access.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
  <property resource="META-INF/build-info.properties" />
  <appender name="STDOUT_JSON" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.LogstashAccessEncoder">
      <!-- META-INF/build-info.propertiesのbuild.versionをversionに使用 -->
      <version>${build.version}</version>
      <fieldNames>
        <!-- デフォルトのフィールド名を上書き -->
        <timestamp>timestamp</timestamp>
        <version>version</version>
     <!-- デフォルトでHeader情報は含まれないので出力したい場合は指定する -->
        <requestHeaders>request_headers</requestHeaders>
        <responseHeaders>response_headers</responseHeaders>
      </fieldNames>
      <!-- フィールドを追加する場合 -->
      <customFields>{"custom_key": "custom_value"}</customFields>
      <!-- 改行付きで表示したい時用 -->
      <jsonGeneratorDecorator
        class="net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator"/>
    </encoder>
  </appender>
  <appender-ref ref="STDOUT_JSON"/>
</configuration>

柔軟に出力する場合

柔軟にフォーマットを変更したい場合はAccessEventCompositeJsonEncoderを使用します。

logback-access.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
  <property resource="META-INF/build-info.properties" />
  <appender name="STDOUT_JSON" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.AccessEventCompositeJsonEncoder">
      <!-- 改行付きで表示したい時用 -->
       <jsonGeneratorDecorator
         class="net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator"/>
      <providers>
        <pattern>
          <pattern>
            {
            "timestamp": "%t{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}",
            "version": "${build.version}",
            "method": "%m",
            "status": "%s",
            "host": "%h",
            "path": "%U",
            "query": "%q",
            "request_size": "%i{Content-Length}",
            "user_agent": "%i{User-Agent}",
            "remote_address": "%i{REMOTE_ADDR}",
            "referer": "%i{Referer}",
            "protocol": "%H",
            "latency": "%D",
            "remote_ip": "%a"
            }
         </pattern>
        </pattern>
      </providers>
    </encoder>
  </appender>
  <appender-ref ref="STDOUT_JSON"/>
</configuration>

出力可能な項目一覧: http://logback.qos.ch/xref/ch/qos/logback/access/PatternLayout.html

参考記事

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?