How to Dynamically Change Log Levels and Refresh Config in Spring Boot 3 Without Restart
This article demonstrates how to modify Spring Boot 3 log levels and refresh the application.yml configuration on the fly using Actuator endpoints and custom code, providing step‑by‑step examples, code snippets, and screenshots to help developers avoid service restarts.
1. Introduction
In production environments, managing logs and adjusting configuration files in Spring Boot often requires service restarts, which is time‑consuming and impacts availability. This article shows how to change log levels and update application.yml dynamically without restarting the service.
2. Practical Cases
2.1 Dynamic Log Level Modification
Spring Boot Actuator provides an endpoint to change the log level of a specific logger or the root logger at runtime.
Enable all actuator endpoints by adding the following configuration:
<code>management:
endpoints:
web:
base-path: /ac
exposure:
include: '*'</code>After starting the application, the default log level is INFO , so only INFO and ERROR logs are shown.
Modify a logger’s level via the endpoint /ac/loggers/{groupName} . After a successful change, all log levels are output as expected.
You can also list all logger groups and their current levels:
Modifying the ROOT logger controls the global log output level.
2.2 Dynamic application.yml Refresh
Normally changing application.yml requires a restart. By mimicking Spring Cloud’s /refresh mechanism, we can reload configuration in a plain Spring Boot project.
The core code re‑creates the Spring environment and merges the new property sources:
<code>private void updateEnvironment() {
ConfigurableApplicationContext context = null;
try {
Map<String, Object> map = new HashMap<>();
map.put("spring.main.web-application-type", "NONE");
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addFirst(new MapPropertySource("init", map));
SpringApplicationBuilder builder = new SpringApplicationBuilder(Empty.class)
.bannerMode(Banner.Mode.OFF)
.web(WebApplicationType.NONE)
.environment(environment);
context = builder.run();
if (environment.getPropertySources().contains("init")) {
environment.getPropertySources().remove("init");
}
// Get original property sources
MutablePropertySources target = env.getPropertySources();
// Merge new sources
for (PropertySource<?> source : environment.getPropertySources()) {
String name = source.getName();
if (target.contains(name)) {
target.replace(name, source);
} else {
target.addFirst(source);
}
}
} finally {
if (context != null) {
context.close();
}
}
}</code>After refreshing, re‑bind beans that depend on the updated properties:
<code>@Component
@ConfigurationProperties(prefix = "pack.app")
public class AppProperties {
private String title;
private String version;
// getters, setters
}</code>Initial application.yml example:
<code>pack:
app:
title: XXXOOO项目
version: 1.0.0</code>Controller to view and refresh configuration:
<code>@RestController
@RequestMapping("/props")
public class ConfigPropertyController extends BaseController {
@Resource
private AppProperties appProperties;
// View current config
@GetMapping("/app")
public AppProperties app() {
return this.appProperties;
}
// Refresh config
@GetMapping("/refresh")
public Object refresh() {
updateEnvironment();
processBean();
return "refresh success";
}
private void processBean() {
this.beanFactory.initializeBean(appProperties, "appProperties");
}
}</code>Using an event‑driven approach can further decouple the refresh logic.
Conclusion
The article provides a complete guide to dynamically adjust log levels and refresh configuration files in Spring Boot 3, enabling developers to improve operational efficiency and maintain system stability without service downtime.
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.