How to Dynamically Change Spring Boot Log Levels Without Restart
This guide demonstrates how to modify Spring Boot 2.4.12 logger levels at runtime using LoggingSystem, enabling per‑package or whole‑application log level changes without editing configuration files or restarting the service.
Environment: Spring Boot 2.4.12.
When a project needs more detailed logs during runtime, developers often modify the configuration file and restart the service. This article shows how to change log levels dynamically without touching the configuration file or restarting.
Test Controller
package com.pack.logger.change_level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/logger")
public class LoggerLevelChangeController {
private static final Logger logger = LoggerFactory.getLogger(LoggerLevelChangeController.class);
@GetMapping("/print")
public Object print() {
logger.info("This is Info logger...");
logger.debug("This is Debug logger...");
return "print";
}
}The default application.yml config sets the package log level to INFO:
logging:
level:
'[com.pack.logger.change_level]': INFORunning the /logger/print endpoint prints only the INFO message because the logger is set to INFO.
Changing Log Level at Runtime
Add a method that uses LoggingSystem to set the level for the package:
@GetMapping("/level")
public Object changeLevel(String level) {
LoggingSystem system = LoggingSystem.get(ClassLoader.getSystemClassLoader());
// first argument: logger name, second: LogLevel parsed from request
system.setLogLevel("com.pack.logger.change_level", resolveLogLevel(level));
return "change logger level success";
}
private LogLevel resolveLogLevel(String level) {
String trimmedLevel = level.trim();
return LogLevel.valueOf(trimmedLevel.toUpperCase(Locale.ENGLISH));
}Calling /logger/level?level=DEBUG changes the package logger to DEBUG. Subsequent calls to /logger/print now output both INFO and DEBUG messages without restarting the service.
Changing the Whole System Log Level
To adjust the root logger (affecting the entire application), modify the method to accept a logger name parameter. Passing root sets the name to null, which applies the level globally:
@GetMapping("/level")
public Object changeLevel(String name, String level) {
LoggingSystem system = LoggingSystem.get(ClassLoader.getSystemClassLoader());
if (name.equalsIgnoreCase("root")) {
name = null;
}
system.setLogLevel(name, resolveLogLevel(level));
return "change logger level success";
}Calling /logger/level?name=root&level=DEBUG changes the root level to DEBUG, as shown in the following log output:
Disabling Logging
Extend the resolveLogLevel method to treat the value false as LogLevel.OFF:
private LogLevel resolveLogLevel(String level) {
String trimmedLevel = level.trim();
if ("false".equalsIgnoreCase(trimmedLevel)) {
return LogLevel.OFF;
}
return LogLevel.valueOf(trimmedLevel.toUpperCase(Locale.ENGLISH));
}Setting level=false disables logging for the specified logger.
With these endpoints, developers can dynamically control logging granularity in a running Spring Boot application without modifying configuration files or restarting the service.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
