Skip to content

設定

このページでは、logback-access-spring-boot-starterで使用可能なすべての設定オプションを説明します。

アプリケーションプロパティ

application.ymlまたはapplication.propertiesでSpring Bootプロパティを使用して設定します:

yaml
logback:
  access:
    enabled: true
    # config-location: classpath:custom-access.xml  # classpath: および file: プレフィックスに対応
    local-port-strategy: server
    tomcat:
      # request-attributes-enabled: true  # RemoteIpValveの存在から自動判定
    tee-filter:
      enabled: false
      # include-hosts: localhost,example.com
      # exclude-hosts: internal.example.com
      # max-payload-size: 65536
      # allowed-content-types:
      #   - "text/*"
      #   - "application/json"
    filter:
      # include-url-patterns:
      #   - /api/.*
      exclude-url-patterns:
        - /actuator/.*
        - /health

プロパティリファレンス

プロパティデフォルト説明
logback.access.enabledtrueアクセスロギングの有効/無効
logback.access.config-location自動検出logback-access設定ファイルへのパス。classpath:およびfile: URLプレフィックスに対応
logback.access.local-port-strategyserverポート解決戦略: serverまたはlocal
logback.access.tomcat.request-attributes-enabled自動検出Tomcatリクエスト属性の有効化。未設定時、RemoteIpValveの存在から自動判定
logback.access.tee-filter.enabledfalseリクエスト/レスポンスボディキャプチャの有効化
logback.access.tee-filter.include-hostsnull(全ホスト)含めるホストのカンマ区切りリスト
logback.access.tee-filter.exclude-hostsnull(なし)除外するホストのカンマ区切りリスト
logback.access.tee-filter.max-payload-size65536ログ出力する最大ペイロードサイズ(バイト)
logback.access.tee-filter.allowed-content-typesnullボディキャプチャを許可するContent-Typeパターン(上書きモード)
logback.access.filter.include-url-patternsnull含めるURLパターン(正規表現)
logback.access.filter.exclude-url-patternsnull除外するURLパターン(正規表現)

設定ファイルの解決

logback.access.config-locationが設定されている場合、そのパスを直接使用します(フォールバックなし)。指定されたファイルが存在しない場合、アプリケーションの起動に失敗しエラーが発生します。

未設定の場合、以下の順序で設定ファイルを検索します:

  1. classpath:logback-access-test.xml(テスト用)
  2. classpath:logback-access.xml
  3. classpath:logback-access-test-spring.xml(Spring機能付きテスト用)
  4. classpath:logback-access-spring.xml
  5. 組み込みフォールバック設定

XML設定

基本構造

xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- Appenderはログの出力先を定義 -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%h %l %u [%t] "%r" %s %b</pattern>
        </encoder>
    </appender>

    <!-- Appenderを参照して有効化 -->
    <appender-ref ref="console"/>
</configuration>

Springプロパティの使用

設定にSpringプロパティを注入:

xml
<configuration>
    <springProperty name="appName" source="spring.application.name"
                    defaultValue="app" scope="context"/>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[${appName}] %h %l %u [%t] "%r" %s %b</pattern>
        </encoder>
    </appender>
    <appender-ref ref="console"/>
</configuration>

デフォルトスコープ

<springProperty>のデフォルトスコープはLOCALです。LOCALスコープのプロパティは、XML設定処理中の変数置換(${varName}など)でのみ使用可能です。context.getProperty()でプログラム的にアクセスするには、scope="context"を設定してください。

Springプロファイルの使用

環境別に異なるAppenderを設定:

xml
<configuration>
    <springProfile name="dev">
        <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%h %l %u [%t] "%r" %s %b %D</pattern>
            </encoder>
        </appender>
        <appender-ref ref="console"/>
    </springProfile>

    <springProfile name="prod">
        <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>logs/access.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>logs/access.%d{yyyy-MM-dd}.log</fileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>%h %l %u [%t] "%r" %s %b</pattern>
            </encoder>
        </appender>
        <appender-ref ref="file"/>
    </springProfile>
</configuration>

プロファイル式

Springプロファイル式は否定と複数プロファイルをサポート:

xml
<!-- 本番環境以外でアクティブ -->
<springProfile name="!prod">
    ...
</springProfile>

<!-- devまたはstagingでアクティブ -->
<springProfile name="dev, staging">
    ...
</springProfile>

File Appender

アクセスログをファイルに出力:

xml
<appender name="file" class="ch.qos.logback.core.FileAppender">
    <file>logs/access.log</file>
    <encoder>
        <pattern>%h %l %u [%t] "%r" %s %b</pattern>
    </encoder>
</appender>

Rolling File Appender

時間またはサイズに基づいてログをローテーション:

xml
<appender name="rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logs/access.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <fileNamePattern>logs/access.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
        <maxFileSize>100MB</maxFileSize>
        <maxHistory>30</maxHistory>
        <totalSizeCap>3GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
        <pattern>%h %l %u [%t] "%r" %s %b</pattern>
    </encoder>
</appender>

アクセスロギングの無効化

プロパティを設定してアクセスロギングを無効化:

yaml
logback:
  access:
    enabled: false

またはSpringプロファイルを使用:

yaml
spring:
  profiles:
    active: test

---
spring:
  config:
    activate:
      on-profile: test

logback:
  access:
    enabled: false

関連ページ

  • Tomcat連携 — Tomcat固有のプロパティとリバースプロキシの設定
  • Jetty連携 — Jetty固有の動作と既知の制限事項
  • 高度な設定 — TeeFilter、URLフィルタリング、JSONロギング、Spring Security

Released under the Apache 2.0 License.