How to Change Logging Level in Spring Boot at Runtime
Logging is essential in observing the behavior and performance of applications. However, static configurations can sometimes become a roadblock during runtime troubleshooting. Dynamic control over logging levels allows developers and operators to adjust logging verbosity without restarting the application, making debugging significantly more efficient in production environments.
This article explores how to change logging levels in Spring Boot applications at runtime using Spring Boot Actuator and provides actionable examples to illustrate the process.
Why Runtime Logging Control is Useful
Dynamic runtime logging provides several benefits:
- Immediate Feedback – Switching to more verbose logging during runtime can help diagnose problems without waiting for the next deployment cycle.
- Operational Flexibility – Developers can focus on specific components by tuning their logging levels without affecting global logs.
- Reduced Downtime – Avoids restarting production applications to apply new logging settings, ensuring uninterrupted operations.
- Efficient Resource Usage – Limits verbose logging to specific classes or packages, reducing log volume and storage impact.
With Spring Boot and its rich Actuator features, adjusting logging levels at runtime becomes straightforward.
Exposing Spring Boot Actuator /loggers
Endpoint
Spring Boot’s Actuator module provides several management endpoints, including the /loggers
endpoint, which is specifically designed for logging configuration.
1. Add Spring Boot Actuator to the Project:
If Actuator isn’t already part of your application, include it in the pom.xml
(for Maven projects):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
For Gradle-based projects, add:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
2. Enable the /loggers
Endpoint:
The /loggers
endpoint isn’t exposed by default. To enable it, add the following property in application.properties
:
management.endpoints.web.exposure.include=loggers
Alternatively, for YAML configuration:
management: endpoints: web: exposure: include: loggers
3. Access the /loggers
Endpoint:
After enabling the endpoint, you can access it at:
http://<host>:<port>/actuator/loggers
This endpoint provides an overview of all available loggers and their currently configured levels. Each logger is associated with a specific package or class.
Changing Log Level via REST API
Using the /loggers
endpoint allows you to dynamically modify logging levels by sending HTTP POST requests. Below is the step-by-step process to change logging levels.
Example of Changing a Logger’s Level:
Suppose you want to enable DEBUG
level logging for the package com.example
.
- Send a POST Request:
Use a REST client, such as curl
or Postman, to send the following request:
curl -X POST \ -H "Content-Type: application/json" \ -d '{"configuredLevel": "DEBUG"}' \ http://<host>:<port>/actuator/loggers/com.example
- Verify the Change:
Access the /loggers
endpoint to confirm:
curl http://<host>:<port>/actuator/loggers/com.example
Example response:
{ "configuredLevel": "DEBUG", "effectiveLevel": "DEBUG" }
The configuredLevel
indicates the level you set explicitly, while effectiveLevel
represents the active level after considering inheritance from parent loggers.
Reverting Log Level Changes:
Reverting to the default level can be done by omitting the level in the payload:
curl -X POST \ -H "Content-Type: application/json" \ -d '{"configuredLevel": null}' \ http://<host>:<port>/actuator/loggers/com.example
This restores the logging level to its inherited value from the root logger or an ancestor in the package hierarchy.
Security of Actuator Endpoints
Actuator endpoints grant powerful control over your application. It’s crucial to secure these endpoints to avoid unauthorized access.
Best Practices for Endpoint Security:
- Restrict Access to Authorized Users:
Use Spring Security to protect the /actuator/loggers
endpoint. For example:
management.endpoint.loggers.roles=ACTUATOR_ADMIN
- Enable HTTPS:
Always serve Actuator endpoints over HTTPS to protect sensitive data.
- IP Whitelisting:
Restrict access to /actuator
endpoints by configuring access control lists (ACLs) based on IP addresses.
- Disable Unused Endpoints:
Disable any Actuator endpoints that are not required in production.
Viewing All Configured Loggers
The /loggers
endpoint displays all available loggers and their configuration. To view the complete list:
curl http://<host>:<port>/actuator/loggers
This provides a JSON response containing each logger’s name, configuredLevel
, and effectiveLevel
, helping you understand the current logging setup.
Sample Response:
{ "levels": ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"], "loggers": { "com.example": { "configuredLevel": "DEBUG", "effectiveLevel": "DEBUG" }, "org.springframework": { "configuredLevel": null, "effectiveLevel": "INFO" } } }
Persisting Log Level After Restart (Optional)
By default, any runtime changes to logging configuration are not persisted. To retain changes after an application restart:
- Update the Configuration File:
Add the updated log level to your application.properties
or application.yml
. For example:
logging.level.com.example=DEBUG
- Automate Using External Configurations:
Use centralized configuration management tools like Spring Cloud Config if you manage multiple microservices.
Recap with curl
and Postman Demo
Here’s a demonstration flow using curl
:
- View Logger Details:
curl http://<host>:<port>/actuator/loggers/com.example
- Set Logger Level to DEBUG:
curl -X POST \ -H "Content-Type: application/json" \ -d '{"configuredLevel": "DEBUG"}' \ http://<host>:<port>/actuator/loggers/com.example
- Check Logger Level:
curl http://<host>:<port>/actuator/loggers/com.example
- Revert Logger Level:
curl -X POST \ -H "Content-Type: application/json" \ -d '{"configuredLevel": null}' \ http://<host>:<port>/actuator/loggers/com.example
Using Postman, you can achieve this using:
- GET requests to check logger levels.
- POST requests with a JSON payload to adjust logging levels.
Final Thoughts
Runtime logging control in Spring Boot applications significantly enhances flexibility and responsiveness during troubleshooting. By leveraging the /loggers
endpoint from Spring Boot Actuator, you can dynamically fine-tune logging configurations without the need for redeployment. For production environments, ensure that Actuator endpoints are securely configured to prevent unauthorized changes. Combine these strategies with persistent configurations to build a robust logging setup that meets both debugging and operational requirements.