Spring / Spring Boot
How to change log levels without restart using spring boot actuator?
You can change log levels in a running Spring Boot application without a restart
using the Spring Boot Actuator's /actuator/loggers endpoint.
This allows you to dynamically view and modify logging configurations via a simple HTTP request.
Steps to Implement and Use Dynamic Logging
1. Add the Actuator Dependency
Include the Spring Boot Actuator dependency in your project's
pom.xml (Maven) or build.gradle (Gradle) file.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2. Expose the Loggers Endpoint
To expose the loggers endpoint, configure
management.endpoints.web.exposure.include
in your application.properties or application.yml file.
3. Change Log Levels at Runtime
Use GET requests to view current logger levels and POST requests to change them.
View current logger levels:
GET http://localhost:8080/actuator/loggers
Change a specific logger level:
POST /actuator/loggers/{name} { "configuredLevel": "DEBUG" }
Setting configuredLevel to null resets the logger to its default level.
Important Considerations
- Security: Secure Actuator endpoints, especially in production, due to the sensitive nature of the information and control they provide.
- Persistence: Changes made via Actuator are temporary and will be lost on application restart.
- Centralized Management: Consider tools like Spring Boot Admin for managing log levels across multiple microservice instances.
More Related questions...