Spring Cloud Spring Boot mybatis distributed microservice cloud architecture (44) Dynamically modify the log level (1)

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-actuatorautomatically open the function of this endpoint when dependencies are introduced (for more details about the spring-boot-starter-actuatormodule, 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.xmlfollowing 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.propertiesadd a configuration to close the security authentication verification.

    management.security.enabled=false
    

    Otherwise, when accessing /loggersthe 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"
    }

    source code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856401&siteId=291194637