1. 序章

このチュートリアルでは、実行時にSpring Bootアプリケーションのログレベルを変更する方法を見ていきます。 多くのものと同様に、 Spring Bootには、それを構成するロギング機能が組み込まれています。 実行中のアプリケーションのログレベルを調整する方法を検討します。

これを行う3つの方法を見ていきます。Spring Bootアクチュエータロガーエンドポイントの使用、ログバックの自動スキャン機能、そして最後にSpring Boot管理者の使用です。 ツール。

2. スプリングブートアクチュエータ

/ loggers Actuatorエンドポイントを使用して、ログレベルを表示および変更することから始めます。 /loggersエンドポイントはアクチュエーター/ロガーで利用可能であり、パスの一部として名前を追加することで特定のロガーにアクセスできます。

たとえば、URL http:// localhost:8080 / actuator / loggers /rootを使用してルートロガーにアクセスできます。

2.1. 設定

Spring Bootアクチュエータを使用するようにアプリケーションを設定することから始めましょう。

まず、 Spring BootActuatorMaven依存関係pom.xmlファイルに追加する必要があります。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.4.0</version>
</dependency>

Spring Boot 2.x以降、ほとんどのエンドポイントはデフォルトで無効になっています。そのため、application.propertiesで/ロガーエンドポイントも有効にする必要があります。 ] ファイル:

management.endpoints.web.exposure.include=loggers
management.endpoint.loggers.enabled=true

最後に、一連のロギングステートメントを使用してコントローラーを作成し、実験の効果を確認できるようにします。

@RestController
@RequestMapping("/log")
public class LoggingController {
    private Log log = LogFactory.getLog(LoggingController.class);

    @GetMapping
    public String log() {
        log.trace("This is a TRACE level message");
        log.debug("This is a DEBUG level message");
        log.info("This is an INFO level message");
        log.warn("This is a WARN level message");
        log.error("This is an ERROR level message");
        return "See the log for details";
    }
}

2.2. /ロガーエンドポイントの使用

アプリケーションを起動して、ログAPIにアクセスしましょう。

curl http://localhost:8080/log

次に、3つのロギングステートメントを見つける必要があるログを確認しましょう。

2019-09-02 09:51:53.498  INFO 12208 --- [nio-8080-exec-1] c.b.s.b.m.logging.LoggingController      : This is an INFO level message
2019-09-02 09:51:53.498  WARN 12208 --- [nio-8080-exec-1] c.b.s.b.m.logging.LoggingController      : This is a WARN level message
2019-09-02 09:51:53.498 ERROR 12208 --- [nio-8080-exec-1] c.b.s.b.m.logging.LoggingController      : This is an ERROR level message

次に、/ loggers Actuatorエンドポイントを呼び出して、com.baeldung。spring.boot.management.loggingパッケージのログレベルを確認します。

curl http://localhost:8080/actuator/loggers/com.baeldung.spring.boot.management.logging
  {"configuredLevel":null,"effectiveLevel":"INFO"}

ロギングレベルを変更するには、POSTリクエストを/loggersエンドポイントに発行できます。

curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "TRACE"}'
  http://localhost:8080/actuator/loggers/com.baeldung.spring.boot.management.logging
  HTTP/1.1 204
  Date: Mon, 02 Sep 2019 13:56:52 GMT

ロギングレベルを再度確認すると、TRACEに設定されていることがわかります。

curl http://localhost:8080/actuator/loggers/com.baeldung.spring.boot.management.logging
  {"configuredLevel":"TRACE","effectiveLevel":"TRACE"}

最後に、ログAPIを再実行して、実際の変更を確認できます。

curl http://localhost:8080/log

それでは、ログをもう一度確認しましょう。

2019-09-02 09:59:20.283 TRACE 12208 --- [io-8080-exec-10] c.b.s.b.m.logging.LoggingController      : This is a TRACE level message
2019-09-02 09:59:20.283 DEBUG 12208 --- [io-8080-exec-10] c.b.s.b.m.logging.LoggingController      : This is a DEBUG level message
2019-09-02 09:59:20.283  INFO 12208 --- [io-8080-exec-10] c.b.s.b.m.logging.LoggingController      : This is an INFO level message
2019-09-02 09:59:20.283  WARN 12208 --- [io-8080-exec-10] c.b.s.b.m.logging.LoggingController      : This is a WARN level message
2019-09-02 09:59:20.283 ERROR 12208 --- [io-8080-exec-10] c.b.s.b.m.logging.LoggingController      : This is an ERROR level message

3. ログバック自動スキャン

デフォルトでは、Spring Bootアプリケーションはログバックログライブラリを使用しています。次に、ログバックの自動スキャン機能を利用してログレベルを変更する方法を見てみましょう。

まず、 src / main /resourcesディレクトリの下にlogback.xmlという名前のファイルを配置して、ログバック構成を追加しましょう。

<configuration scan="true" scanPeriod="15 seconds">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="com.baeldung.spring.boot.management.logging" level="INFO" />

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

重要な詳細は、logback.xmlファイルの最初の行にあります。 scan属性をtrueに設定すると、構成ファイルの変更を確認するようにLogbackに指示します。 自動スキャンは、デフォルトでは60秒ごとに実行されます。

scanPeriod を15秒に設定すると、15秒ごとにリロードするように指示されるため、実験中に長く待つ必要はありません。

アプリケーションを起動し、ログAPIを再度呼び出して試してみましょう。

curl http://localhost:8080/log

出力には、パッケージのINFOログレベルが反映されている必要があります。

10:21:13.167 [http-nio-8080-exec-1] INFO  c.b.s.b.m.logging.LoggingController - This is an INFO level message
10:21:13.167 [http-nio-8080-exec-1] WARN  c.b.s.b.m.logging.LoggingController - This is a WARN level message
10:21:13.168 [http-nio-8080-exec-1] ERROR c.b.s.b.m.logging.LoggingController - This is an ERROR level message

それでは、logback.xmlcom.baeldung。spring.boot.management.loggingロガーをTRACEに変更しましょう。

<logger name="com.baeldung.spring.boot.management.logging" level="TRACE" />

15秒待ってから、 http:// localhost:8080 / log でログAPIを再実行し、ログ出力を確認しましょう。

10:24:18.429 [http-nio-8080-exec-2] TRACE c.b.s.b.m.logging.LoggingController - This is a TRACE level message
10:24:18.430 [http-nio-8080-exec-2] DEBUG c.b.s.b.m.logging.LoggingController - This is a DEBUG level message
10:24:18.430 [http-nio-8080-exec-2] INFO  c.b.s.b.m.logging.LoggingController - This is an INFO level message
10:24:18.430 [http-nio-8080-exec-2] WARN  c.b.s.b.m.logging.LoggingController - This is a WARN level message
10:24:18.430 [http-nio-8080-exec-2] ERROR c.b.s.b.m.logging.LoggingController - This is an ERROR level message

4. Spring Boot Admin

ログレベルを変更する3番目の方法は、Spring Boot管理ツールを使用することです。 Spring Boot Adminを使用するには、サーバーアプリケーションを作成し、アプリケーションをクライアントとして構成する必要があります。

4.1. 管理アプリケーション

Spring Boot Adminでログレベルを変更するには、管理サーバーとして使用する新しいアプリケーションを設定する必要があります。 そのためにSpringInitialzrを使用できます。

最新のspring-boot-admin-starter-serverをpom.xmlに追加しましょう。

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.4.1</version>
</dependency>

管理サーバーの設定の詳細な手順については、 Spring BootAdminガイドのセクション2を参照してください。 また、セクション4には、クライアントを保護するため、セキュリティを設定するために必要な情報が含まれています。

4.2. クライアント構成

管理サーバーを作成したら、アプリケーションをクライアントとして設定する必要があります。

まず、 spring-boot-admin-starter-clientのMaven依存関係を追加しましょう。

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.4.1</version>
</dependency>

管理サーバーとクライアント間のセキュリティも必要になるため、Spring Bootセキュリティスターターを導入しましょう。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>2.4.0</version>
</dependency>

次に、application.propertiesファイルでいくつかの構成変更を行う必要があります。

管理サーバーはポート8080で実行されているので、ポートを変更してアプリケーションに名前を付けることから始めましょう。

spring.application.name=spring-boot-management
server.port=8081

次に、サーバーにアクセスするために必要な構成を追加しましょう。

spring.security.user.name=client
spring.security.user.password=client

spring.boot.admin.client.url=http://localhost:8080
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin

spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name}
spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password}

これには、管理サーバーが実行されているURLと、クライアントと管理サーバーの両方のログイン情報が含まれます。

最後に、管理サーバーがクライアントのステータスを判別できるように、/ health、/ info 、および/metricsアクチュエータエンドポイントを有効にする必要があります。

management.endpoints.web.exposure.include=httptrace,loggers,health,info,metrics

ロガーレベルの変更はPOST操作であるため、アクチュエータエンドポイントのCSRF保護を無視するために、セキュリティ構成を少し追加する必要もあります。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().ignoringAntMatchers("/actuator/**");
}

4.3. SpringBootAdminの使用

構成が邪魔にならないように、 mvn spring -boot:runを使用してクライアントとサーバーアプリケーションの両方を起動しましょう。

何も変更せずに、 http:// localhost:8081 /logでログAPIにアクセスすることから始めましょう。 現在セキュリティが有効になっているため、application.propertiesで指定した資格情報を使用してログインするように求められます。

ログ出力には、INFOログレベルを反映するログメッセージが表示されます。

09:13:23.416 [http-nio-8081-exec-10] INFO  c.b.s.b.m.logging.LoggingController - This is an INFO level message
09:13:23.416 [http-nio-8081-exec-10] WARN  c.b.s.b.m.logging.LoggingController - This is a WARN level message
09:13:23.416 [http-nio-8081-exec-10] ERROR c.b.s.b.m.logging.LoggingController - This is an ERROR level message

それでは、Spring Boot管理サーバーにログインして、ログレベルを変更しましょう。 http:// localhost:8080 に移動し、管理者の資格情報を使用してログインします。 spring-boot-managementアプリケーションが表示される登録済みアプリケーションのリストが表示されます。

spring -boot-management を選択し、左側のメニューを使用してロガーを表示しましょう。

com.baeldung。spring.boot.management.loggingロガーがINFOに設定されています。 それをTRACEに変更して、ログAPIを再実行しましょう。

これで、ログ出力に新しいロガーレベルが反映されます。

10:13:56.376 [http-nio-8081-exec-4] TRACE c.b.s.b.m.logging.LoggingController - This is a TRACE level message
10:13:56.376 [http-nio-8081-exec-4] DEBUG c.b.s.b.m.logging.LoggingController - This is a DEBUG level message
10:13:56.376 [http-nio-8081-exec-4] INFO  c.b.s.b.m.logging.LoggingController - This is an INFO level message
10:13:56.376 [http-nio-8081-exec-4] WARN  c.b.s.b.m.logging.LoggingController - This is a WARN level message
10:13:56.376 [http-nio-8081-exec-4] ERROR c.b.s.b.m.logging.LoggingController - This is an ERROR level message

5. 結論

この記事では、実行時にロギングレベルを制御するさまざまな方法について説明しました。 組み込みのアクチュエータ機能を使用することから始めました。 その後、Logbackの自動スキャン機能を使用しました。

最後に、Spring Boot Adminを使用して、登録済みのクライアントアプリケーションのログレベルを監視および変更する方法を学びました。

アクチュエータとログバックを使用するためのサンプルコードとSpringBoot Admin を設定するためのサンプルコードは、どちらもGitHubで入手できます。