如何在运行时更改日志级别而不重新启动弹簧引导应用程序

2022-08-31 17:12:16

我已经在PCF中部署了弹簧靴应用程序。我想根据环境变量记录消息。我应该怎么做才能在不重新启动应用程序的情况下运行运行时日志级别更改?


答案 1

在 Spring Boot 1.5+ 中更改日志级别可以通过 http 端点完成

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

并且比您可以使用

curl -X "POST" "http://localhost:8080/loggers/de.springbootbuch" \
     -H "Content-Type: application/json; charset=utf-8" \
     -d $'{
  "configuredLevel": "WARN"
}'  

其中 /loggers/ 之外的所有内容都是记录器的名称。

如果你在PCF中运行它,它会变得更好:这是从他们的后端直接支持的。


答案 2

对于弹簧靴 2.1.5+:

首先,您需要执行器插件:

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

其次,您需要公开端点,就像Dennis在他的评论中所说的那样(默认情况下处于禁用状态):loggers

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

最后,可以使用 Rest 终结点获取有关记录器的信息并设置日志记录级别。

curl -X "GET" "http://localhost:8080/actuator/loggers"

设置可以使用的日志记录级别Root

curl -X "POST" "http://localhost:8080/actuator/loggers/ROOT" -H "Content-Type: application/json; charset=utf-8"   -d $'{ "configuredLevel": "INFO" }'

推荐