loggers endpoint
In this article, we will take a look at a new control endpoint introduced in Spring Boot 1.5.x: /loggers
, which will provide us with a powerful function to dynamically modify the log level of Spring Boot applications. The use of this function is very simple, and it still continues the implementation of Spring Boot's automatic configuration, so it only needs to spring-boot-starter-actuator
automatically open the function of this endpoint when dependencies are introduced (for more details about the spring-boot-starter-actuator
module, see: "Spring Boot Actuator" Monitoring Endpoints Summary" article).
Let's take a look at how to use this feature with a practical example:
-
Build a basic Spring Boot application. If you are not familiar with how to build, you can refer to the article "Using Spring Initializr in Intellij to Quickly Build Spring Boot/Cloud Projects" .
-
Introduce the
pom.xml
following dependencies (if you use Spring Initializr in Intellij, you can directly select the web and actuator modules in the prompt box).<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
Add an interface to the main application class to test log level changes, such as the following implementation:
@RestController @SpringBootApplication public class DemoApplication { private Logger logger = LoggerFactory.getLogger(getClass()); @RequestMapping(value = "/test", method = RequestMethod.GET) public String testLogLevel() { logger.debug("Logger Level :DEBUG"); logger.info("Logger Level :INFO"); logger.error("Logger Level :ERROR"); return ""; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
For the success of the subsequent test,
application.properties
add a configuration to close the security authentication verification.management.security.enabled=false
Otherwise, when accessing
/loggers
the endpoint, the following error will be reported:{ "timestamp": 1485873161065, "status": 401, "error": "Unauthorized", "message": "Full authentication is required to access this resource.", "path": "/loggers/com.didispace" }