Spring Boot動的設定ログレベル

12062 ワード

要約Spring Bootはログの構成上、多くの自動化作業を行いました.1.5.1のバージョンからspring-boot-starter-actuatorに基づくエンドポイント/loggersが提供される.このエンドポイントによって、システムを参照するpackage-pathのログレベルと、実行中のアプリケーションのログレベルを特定のpackage-pathに設定する機能を実現することができる.
Actutor依存
pom依存
WebベースのプロジェクトとActutorが提供するエンドポイントを利用して構成するため、依存性が必要です.
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-actuatorartifactId>
dependency>
セキュリティ設定Actuatorが提供するエンドポイントは、デフォルトではセキュリティ認証が必要であってこそアクセスできる.中には敏感な情報が含まれているからです.セキュリティ認証が必要な場合は、構成Spring-Securityが必要です.便利さのためには、まずセキュリティ権限が必要ではないように設定します.
management.security.enabled=false
GET要求アクセスGETを送信して、サポートされたログレベルを取得するように要求しても良いし、システムのデフォルトログなど、各パケットパスに対応するログレベルを取得しても良い.
{
  levels: [
    "OFF",
    "ERROR",
    "WARN",
    "INFO",
    "DEBUG",
    "TRACE"
  ],
  loggers: {
    ROOT: {
      configuredLevel: "INFO",
      effectiveLevel: "INFO"
    },
    cn: {
      configuredLevel: null,
      effectiveLevel: "INFO"
    },
    cn.cincout: {
      configuredLevel: null,
      effectiveLevel: "INFO"
    },
    cn.cincout.tech: {
      configuredLevel: null,
      effectiveLevel: "INFO"
    },
    cn.cincout.tech.black: {
      configuredLevel: null,
      effectiveLevel: "INFO"
    },
    cn.cincout.tech.black.hole: {
      configuredLevel: null,
      effectiveLevel: "INFO"
    },
    cn.cincout.tech.black.hole.springboot: {
      configuredLevel: null,
      effectiveLevel: "INFO"
    },
    cn.cincout.tech.black.hole.springboot.SpringBootStartApplication: {
      configuredLevel: null,
      effectiveLevel: "INFO"
    },
    cn.cincout.tech.black.hole.springboot.application: {
      configuredLevel: null,
      effectiveLevel: "INFO"
    }
  }
}
ログレベルの設定
ログ出力クラスを作成http://localhost:8080/loggersを作成して、各レベルのログを出力します.
package cn.cincout.tech.black.hole.springboot.interfaces.web;

@Controller
public class HomeController {
    private final static Logger LOG = LoggerFactory.getLogger(HomeController.class);
    @Value("${val.name}")
    private String name;
    @Value("${val.password}")
    private String password;

    @GetMapping(value = {"", "/"})
    @ResponseBody
    public Map home() {
        LOG.trace("trace level log");
        LOG.debug("debug level log");
        LOG.info("info level log");
        LOG.warn("warn level log");
        LOG.error("error level log");

        Map result = new HashMap<>();
        result.put("status", "good");
        result.put("name", name);
        result.put("password", password);
        return result;
    }

    @GetMapping(value = "/admin")
    public String shutdown() {
        return "shutdown";
    }
}
ログのレベルを表示
アプリケーションアクセスを開始したcontrollerは、
2017-09-01 10:19:16.180  INFO 6954 --- [nio-8443-exec-4] c.c.t.b.h.s.i.web.HomeController         : info level log
2017-09-01 10:19:16.180  WARN 6954 --- [nio-8443-exec-4] c.c.t.b.h.s.i.web.HomeController         : warn level log
2017-09-01 10:19:16.180 ERROR 6954 --- [nio-8443-exec-4] c.c.t.b.h.s.i.web.HomeController         : error level log
http://localhost:8080/のデフォルトのROOTログレベルはSpring Bootです.
特定のパケットのログレベルを設定します.INFOエンドポイントから提供された/loggers要求によって、パケットパスPOSTのログレベルが変更されたのはcn.cincout.tech.black.hole.springboot.interfaces.web*である.DEBUGを送信してPOSTに要求します.ここで、Bodyを要求する内容は以下の通りです.
{
  "configuredLevel": "DEBUG"
}
  • GETアクセス/loggers/cn.cincout.tech.black.hole.springboot.interfaces.webは現在のログレベルを調べます.
  • {
      configuredLevel: "DEBUG",
      effectiveLevel: "DEBUG"
    }
  • 再度アクセスして/loggers/cn.cincout.tech.black.hole.springboot.interfaces.webを得ました.
  • 2017-09-01 11:15:43.695 DEBUG 6954 --- [nio-8443-exec-1] c.c.t.b.h.s.i.web.HomeController         : debug level log
    2017-09-01 11:15:43.695  INFO 6954 --- [nio-8443-exec-1] c.c.t.b.h.s.i.web.HomeController         : info level log
    2017-09-01 11:15:43.695  WARN 6954 --- [nio-8443-exec-1] c.c.t.b.h.s.i.web.HomeController         : warn level log
    2017-09-01 11:15:43.695 ERROR 6954 --- [nio-8443-exec-1] c.c.t.b.h.s.i.web.HomeController         : error level log
    なお、http://localhost:8080/により構成されたログレベルは、アプリケーションの再起動時にシステムの構成に復帰する.恒久的な設定ログのレベルはやはり/loggersによって構成される必要がある.
    ソースコード
    本プロジェクトのソースコードはlogging.level.package-pathから取得できる.ソースコード
    締め括りをつけるgithubから提供されたログレベルの動的構成機能は、私たちのオンラインアプリケーションのデバッグのための良いメカニズムを提供しています.実際の使用では、Spring Bootが提供するセキュリティ機構に関連して、Spring-Securityが提供する様々なシステムレベルのエンドポイントを保護する必要がある.Javaログについてもっと知りたいのですが、「ルーツロギング-SLF 4 JログFacade概要」を参照してください.
    参照
  • Configure aロギング
  • 原文の住所《Spring Bootダイナミックプロファイルログレベル》
    私のブログhttp://tramp.cincout.cn/