How to Dynamically Change Log Levels in Spring Boot 1.5 Using the /loggers Endpoint

This guide shows how to enable the Spring Boot 1.5.x /loggers actuator endpoint, configure a simple application, and use POST and GET requests to modify and inspect log levels at runtime without restarting the service.

Programmer DD
Programmer DD
Programmer DD
How to Dynamically Change Log Levels in Spring Boot 1.5 Using the /loggers Endpoint

Overview

Spring Boot 1.5.x introduced a new actuator endpoint /loggers that allows developers to change the logging level of a running application on the fly. The endpoint is automatically enabled when the spring-boot-starter-actuator dependency is present.

Project Setup

Start with a basic Spring Boot project (for example, generated via Spring Initializr) and add the following dependencies to pom.xml:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.1.RELEASE</version>
  <relativePath/>
</parent>

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

Include a simple REST controller that logs messages at different levels:

@RestController
@SpringBootApplication
public class DemoApplication {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String testLogLevel() {
        logger.debug("Logger Level :DEBUG");
        logger.info("Logger Level :INFO");
        logger.error("Logger Level :ERROR");
        return "";
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

To avoid the default security check that blocks the actuator endpoints, add the following line to application.properties:

management.security.enabled=false

Testing the Application

Run the application and call /test. The console will show INFO and ERROR messages (DEBUG is not printed because the default level is INFO):

2017-01-31 22:34:57.123  INFO 16372 --- [nio-8000-exec-1] ... : Logger Level :INFO
2017-01-31 22:34:57.124  ERROR 16372 --- [nio-8000-exec-1] ... : Logger Level :ERROR

Changing Log Level via POST

Send a POST request to /loggers/com.didispace with the JSON body {"configuredLevel": "DEBUG"}. The response confirms the change:

{
  "configuredLevel": "DEBUG"
}

After the change, call /test again. The console now includes the DEBUG line:

2017-01-31 22:37:35.252 DEBUG 16372 --- [nio-8000-exec-5] ... : Logger Level :DEBUG
2017-01-31 22:37:35.252  INFO 16372 --- [nio-8000-exec-5] ... : Logger Level :INFO
2017-01-31 22:37:35.252 ERROR 16372 --- [nio-8000-exec-5] ... : Logger Level :ERROR

Viewing Current Log Level via GET

A GET request to /loggers/com.didispace returns the current configuration:

{
  "configuredLevel": "DEBUG",
  "effectiveLevel": "DEBUG"
}

Calling /loggers without a specific package lists all logger configurations (output omitted for brevity).

Key Takeaways

The /loggers actuator endpoint provides a lightweight way to adjust logging levels at runtime, which is useful for troubleshooting production issues without restarting the application.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

spring-bootdynamic loggingloggers
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

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.