Spring Boot動的設定ログレベル
12062 ワード
要約
Actutor依存
pom依存
WebベースのプロジェクトとActutorが提供するエンドポイントを利用して構成するため、依存性が必要です.
ログ出力クラスを作成
アプリケーションアクセスを開始した
特定のパケットのログレベルを設定します. GETアクセス 再度アクセスして
ソースコード
本プロジェクトのソースコードは
締め括りをつける
参照 Configure aロギング 原文の住所《Spring Bootダイナミックプロファイルログレベル》
私のブログhttp://tramp.cincout.cn/
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"
}
/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概要」を参照してください.参照
私のブログhttp://tramp.cincout.cn/