Sub topic 13
Changing log levels at runtime with Actuator
This chapter assumes you can run a Spring Boot web application, you know the log levels (TRACE to ERROR), and you have set levels in application.properties. Properties are read at startup, so changing a level that way means a restart. Spring Boot Actuator lets you change a logger's level while the application runs.
Exposing the loggers endpoint
Add spring-boot-starter-actuator to your build. The loggers endpoint exists once Actuator is present, but it is not exposed over HTTP by default. Expose it explicitly:
management.endpoints.web.exposure.include=health,loggersAfter a restart, /actuator/loggers is available.
Reading levels
A GET request to /actuator/loggers lists every known logger. Each entry has a configuredLevel, which is the level set directly on that logger, and an effectiveLevel, which is the level actually in force after inheritance. A logger with no explicit level inherits from its parent, so configuredLevel is null while effectiveLevel shows the inherited value.
To inspect one logger, append its name:
curl http://localhost:8080/actuator/loggers/com.example.ordersChanging a level
Send a POST with the new level in the body. The change applies immediately and needs no restart:
curl -X POST http://localhost:8080/actuator/loggers/com.example.orders \
-H 'Content-Type: application/json' \
-d '{"configuredLevel": "DEBUG"}'A successful call returns HTTP 204 with no body. Setting a package name changes that package and every logger beneath it that has no level of its own.
To undo the change, send null (or an empty body) as the level. The logger then inherits from its parent again:
curl -X POST http://localhost:8080/actuator/loggers/com.example.orders \
-H 'Content-Type: application/json' \
-d '{"configuredLevel": null}'Gotchas
- Changes are temporary. The new level lives in memory. After a restart the application returns to the levels from your properties or
logback-spring.xml. - Per instance. The request affects only the instance that receives it. With several instances behind a load balancer, you must call each one.
- Security. Anyone who can reach the endpoint can change your logging, and raising levels can increase disk use and expose sensitive data. Do not leave it open to the public. Put Actuator on a separate management port with
management.server.port, or protect it with Spring Security. - Only exposed endpoints respond. A 404 on
/actuator/loggersusually means the endpoint is missing frommanagement.endpoints.web.exposure.include. - A logger must exist to be useful. You can set a level for any name, but nothing is logged under a name that no code uses.
A typical workflow
- Production shows an error in the orders code, and the INFO logs do not explain it.
- Set
com.example.orderstoDEBUGthrough the endpoint. - Reproduce the problem and read the detailed logs.
- Reset the level to
nullso the log volume and cost return to normal.
Step 4 matters. DEBUG output left on in production fills disks and slows the application.
Exercise
You POST {"configuredLevel": "DEBUG"} to /actuator/loggers/com.example.orders and get 204. The application is restarted the next day. What is the level of that logger afterwards, and how would you remove the DEBUG setting without restarting?
Show answer
After the restart the level comes from your configuration files (properties or logback-spring.xml), or is inherited from the parent logger, because the runtime change was only kept in memory. To remove it without a restart, POST {"configuredLevel": null} to the same URL, and the logger inherits its parent's level again.
Exercise
Why might /actuator/loggers return 404 even though spring-boot-starter-actuator is on the classpath?
Show answer
The endpoint is not exposed over HTTP by default. Add loggers to management.endpoints.web.exposure.include, for example management.endpoints.web.exposure.include=health,loggers.