Change Spring Boot log level at runtime with actuator

Change Spring Boot log level at runtime with actuator

Spring Boot actuator allows changing an application log at runtime at a granular level. In this article, we cover how to change a Spring Boot application log level at runtime with the actuator endpoint.

The prerequisite for changing the log level at runtime is to expose the logger actuator. We do that by adding the following property to the application.properties or the application.yml file.

Example of application.properties:

management.endpoints.web.exposure.include=loggers

Example of application.yml:

management.endpoints.web.exposure:
  include:
    - loggers

After that, to change the log level, we must make a POST request to the /actuator/loggers endpoint.

To change the entire application log level to debug, we construct this request:

$ curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "DEBUG"}' localhost:8080/actuator/loggers/ROOT

We can even change the log at the package level. For that, we must substitute ROOT with the package name.

$ curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "DEBUG"}' localhost:8080/actuator/loggers/com.madadipouya

Changing the log level permanently

One can change the application log level without calling the actuator endpoint and only by modifying the application property file.

Example of application.properties:

logging.level.root=debug

Example of application.yml:

logging.level:
  root: debug

This approach is not flexible since the application has to be restarted. However, it is intended to make changes permanent, or in environments where exposing the logger actuator is undesirable.

Changing log level from the command line

Lastly, we can change the application log level even without modifying the properties file by just passing the log level to the java -jar command,

$ java -jar myApp.jar --debug

Note that the log level changes do not affect the log format. Many application monitoring services require application logs to be produced in JSON format. We covered that in the enable JSON logging on Spring Boot article.

Conclusion

In this article, we covered how to change a Spring Boot log level at the runtime using the actuator endpoint. While the approach is flexible, it requires exposure to the logger endpoint. In addition to that, the changes are temporary. To make a permanent change, one can directly modify the application properties file. Lastly, we discussed how to modify the application log level from the command line without modifying the properties file.

Inline/featured images credits