Monday, July 23, 2018

SpringBoot2, Update Log Levels At Runtime

I had a requirement where I wanted to create a user interface to dynamically update the spring boot 2 log levels, without having to restart the spring boot service (service)
This can be done by adding the spring boot actuator plugin.
Actuator brings production-ready features to our application.

Heres a good article that explains about the actuator:
http://www.baeldung.com/spring-boot-actuators

More features on how to use the actuator can be found here:

https://docs.spring.io/spring-boot/docs/2.0.x/actuator-api/html/

Note: The actuator service eventually should sit behind a security domain and context so that these services wont be exposed to the public. This article will not cover that right now.


Here's a step by step approach to dynamically set the log levels



1.  Add the actuator library:


     compile("org.springframework.boot:spring-boot-starter-actuator")



2.  In the application properties, add you logging configuration and make sure to enable all the actuator end points.

logging.level.org.springframework.web=ERROR
logging.level.org.hibernate=ERROR
logging.file=testing-features.log
management.endpoints.web.exposure.include=*


3.  Implement a simple controller that will print a log to your log file
@RestController
public class TestController {

    private Logger log = LogManager.getLogger(TestController.class.toString());
    @RequestMapping(value = "/service", method = RequestMethod.GET)
    public ResponseEntity<String> callService(){


        log.info("Service Called");
        return ResponseEntity.ok("Hello World");    }
}

4. View the root loggers log level:
http://localhost:8080/actuator/loggers/root

5. Update the root loggers log level by sending a post request.
Use postman to send a POST request.



No comments:

Post a Comment