Backend Development 7 min read

How to Dynamically Adjust Log Levels in Spring Boot 2.7

This guide explains how to use Spring Boot configuration files and runtime APIs to change log levels on the fly, covering basic log level concepts, a practical controller example, dynamic endpoint implementation, disabling logs, and Spring Cloud integration for flexible logging management.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Dynamically Adjust Log Levels in Spring Boot 2.7

Environment: SpringBoot 2.7.12

1. Introduction

In Spring Boot you can use configuration files (application.properties or application.yml) to adjust log output levels dynamically. Common log levels are TRACE, DEBUG, INFO, WARN, ERROR.

TRACE

DEBUG

INFO

WARN

ERROR

Example of setting log levels in application.yml :

<code>logging:
  level:
    WARN
    org.springframework.web: INFO
    com.pack.xxoo: DEBUG
</code>

Spring Boot also supports advanced configuration to change log levels based on environment variables or system properties, using the Environment object.

Why adjust log levels dynamically?

Debugging and troubleshooting : detailed logs help locate issues during development, while production should limit logs to save resources and avoid exposing sensitive data.

Performance optimization : excessive logging consumes CPU and I/O, affecting performance.

Adapt to different environments : different stages (dev, test, prod) may require different verbosity.

2. Practical Implementation

Initial configuration and definition

Define a controller that prints logs at various levels.

<code>package com.pack.logger_change;

@RestController
@RequestMapping("/logger")
public class LoggerChangeController {
    private static final Logger logger = LoggerFactory.getLogger(LoggerChangeController.class);
    @Resource
    private Environment environment;

    @RequestMapping("/print")
    public String print() {
        logger.info("{}", "这是INFO级别的日志信息");
        logger.debug("{}", "这是DEBUG级别的日志信息");
        logger.error("{}", "这是ERROR级别的日志信息");
        return "专业日志打印";
    }
}
</code>

Configure initial log level (optional):

<code>logging:
  level:
    '[com.pack.logger_change]': ERROR
</code>

Calling /print initially only shows ERROR logs.

Define endpoint to change log level

<code>@Resource
private Environment environment;

@GetMapping("/change")
public Object change(String name, String level) {
    LoggingSystem system = LoggingSystem.get(LoggingSystem.class.getClassLoader());
    setLogLevel(system, name, level);
    return String.format("【%s】级别调整为:%s", name, level);
}

private void setLogLevel(LoggingSystem system, String name, String level) {
    system.setLogLevel(name, LogLevel.valueOf(level.toUpperCase(Locale.ENGLISH)));
}
</code>

Invoke /change to modify the log level of a package, then call /print again. The console now shows INFO and ERROR logs, confirming the dynamic adjustment.

Disable logging

<code>private void setLogLevel(LoggingSystem system, String name, String level) {
    // If level is "false", turn logging off
    if ("false".equalsIgnoreCase(level)) {
        system.setLogLevel(name, LogLevel.OFF);
        return;
    }
    system.setLogLevel(name, LogLevel.valueOf(level.toUpperCase(Locale.ENGLISH)));
}
</code>

In Spring Cloud, the /actuator/loggers endpoint and the EnvironmentChangeEvent can be used to batch‑update log levels via bootstrap.yml .

Summary: Dynamically adjusting log levels in a Spring Boot application provides flexibility for development, testing, and production, helping to balance debugging needs with performance and security considerations.

backendJavadynamic configurationLoggingSpring Boot
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.