Full steps to dynamically configure the Spring Boot log level
- 2021-07-18 07:57:45
- OfStack
Preface
The project uses SpringBoot to build the project. The level of dynamic adjustment log is recorded below.
Endpoint/loggers based on spring-boot-starter-actuator has been available since version 1.5. 1. This endpoint enables you to view the log level of the system's package-path and configure the log level of a running application for a specific package-path.
Actuator Dependence
pom Dependence
Because it is an Web-based project and configured with the endpoints provided by Actuator, you need to rely on:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Security configuration
The endpoint provided by Actuator (Endpoints) requires security authentication before it can be accessed by default. Because it involves some sensitive information. You need to configure Spring-Security if you need security authentication. For convenience, first configure a that does not require security permissions.
management.security.enabled=false
GET Request Access
We can send an GET request to http://localhost: 8091/loggers to get the supported log level, as well as the default log of the system (ROOT) and the individual packet paths (
com.mall.goods.zhongkui
And so on.
{
levels: [
"OFF",
"FATAL",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
loggers: {
ROOT: {
configuredLevel: "INFO",
effectiveLevel: "INFO"
},
com.mall.goods.zhongkui: {
configuredLevel: "DEBUG",
effectiveLevel: "DEBUG"
},
com.mall.goods.zhongkui.mallcpswomai.mapper: {
configuredLevel: "DEBUG",
effectiveLevel: "DEBUG"
}
}
}
Configure log levels
Writing log output classes
Write a log for each level of controller output:
@Slf4j
@Controller
public class TestController {
@GetMapping(value = "/testLog")
public String testLog() {
log.info("---------------------------");
log.debug("debug debug");
log.info("info info info");
log.warn("warn warn warn");
log.error("error error error ");
log.info("---------------------------");
return "ok";
}
}
View Log Levels
Start the application and access http://localhost: 8091/get:
[2018-07-30 18:05:42.868] [http-nio-8091-exec-2] INFO com.mall.goods.zhongkui.mallcpswomai.web.OrderRelationController----------------------------
[2018-07-30 18:05:42.869] [http-nio-8091-exec-2] DEBUG com.mall.goods.zhongkui.mallcpswomai.web.OrderRelationController-debug debug
[2018-07-30 18:05:42.869] [http-nio-8091-exec-2] INFO com.mall.goods.zhongkui.mallcpswomai.web.OrderRelationController-info info info
[2018-07-30 18:05:42.869] [http-nio-8091-exec-2] WARN com.mall.goods.zhongkui.mallcpswomai.web.OrderRelationController-warn warn warn
[2018-07-30 18:05:42.869] [http-nio-8091-exec-2] ERROR com.mall.goods.zhongkui.mallcpswomai.web.OrderRelationController-error error error
[2018-07-30 18:05:42.869] [http-nio-8091-exec-2] INFO com.mall.goods.zhongkui.mallcpswomai.web.OrderRelationController----------------------------
Spring Boot the default ROOT log level is INFO.
Configure the log level for a specific package
Modify the packet path through the POST request provided by/loggers endpoint
com.mall.goods.zhongkui
The log level of is INFO.
* Send an POST request to http://localhost: 8091/com. mall. goods. zhongkui, where the content of the request Body is as follows:
{
"configuredLevel": "INFO"
}
• GET Visit/loggers/com. mall. goods. zhongkui to view the current log level:
{
configuredLevel: "INFO",
effectiveLevel: "INFO"
}
Re-visit http://localhost: 8091/Get: It should be noted that the log level configured by/loggers will return to the system configuration when the application restarts. If you want to permanently configure the log level, you still need to pass the
logging.level.package-path
To configure.
Summarize
The log level dynamic configuration function provided by Spring Boot provides a good mechanism for our online application debugging. In practical use, we need to combine the security mechanism provided by Spring-Security to protect various system-level endpoints provided by Actuator.
Reference
1.Configure a Logger