Configuring Loggers with Spring Boot Actuators

In this tutorial we will learn Spring Boot Actuator Loggers configuration which will let you change the level of verbosity of your logs without restarting Spring Boot applications.

Setting up a sample Spring Boot Application

Firstly, we will set up a sample Spring Boot application that will log a set of statements with different verbosity. Besides your application dependency make sure to include Spring Boot Actuator in it:

Spring Boot Actuator Loggers configuration

Then, set up a sample Controller Class that will log some statements:

package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoggingController {

    Logger logger = LoggerFactory.getLogger(LoggingController.class);

    @RequestMapping("/")
    public String index() {
        logger.trace("TRACE Message");
        logger.debug("DEBUG Message");
        logger.info("INFO Message");
        logger.warn("WARN Message");
        logger.error("ERROR Message");

        return "Hello world!";
    }
}

Next, let’s move to the application.properties file. Within it, we will vary the default (INFO) Logger verbosity for the package com.example.demo. Also, we will expose all Spring Boot actuator endpoints, including Spring Boot Actuator Logger:

logging.level.com.example.demo=WARN
management.endpoints.web.exposure.include=*

Changing the Logger at Runtime with the Actuator

If you try to run the application with the current configuration you will see that the REST Controller prints just the WARN and ERROR statements:

WARN Message  2024-01-16T18:12:10.498+01:00 ERROR 132580 --- [nio-8080-exec-6] com.example.demo.LoggingController       : 
ERROR Message 2024-01-16T18:12:10.681+01:00  WARN 132580 --- [nio-8080-exec-7] com.example.demo.LoggingController 

This is expected. Let’s check the current Actuator Logs from http://localhost:8080/actuator/loggers/com.example.demo

Spring Boot Actuator Loggers configuration

Now, let’s change the Loggers using the Actuator. We will set it to DEBUG for the package com.example.demo :

curl -X POST http://localhost:8080/actuator/loggers/com.example.demo \
     -H 'Content-Type: application/json' \
     -d '{
           "configuredLevel": "DEBUG"
         }'

Then, if you request again the Controller, you will see that the Console displays all messages from DEBUG to ERROR:

2024-01-16T18:24:40.991+01:00 DEBUG 135914 --- [nio-8080-exec-3] com.example.demo.LoggingController       : DEBUG Message
2024-01-16T18:24:40.991+01:00  INFO 135914 --- [nio-8080-exec-3] com.example.demo.LoggingController       : INFO Message
2024-01-16T18:24:40.991+01:00  WARN 135914 --- [nio-8080-exec-3] com.example.demo.LoggingController       : WARN Message
2024-01-16T18:24:40.991+01:00 ERROR 135914 --- [nio-8080-exec-3] com.example.demo.LoggingController       : ERROR Message

The Actuator Logger confirms the current Log Level is now DEBUG:

Conclusion

This article discussed how to change the Logger configuration using Spring Boot Actuator. We have set up a sample Spring Boot application with a custom Logger. Then, we have sent a POST request to the Logger Actuator to modify the Logger Level.

To learn more about Spring Boot 3 Actuator, check this article: Spring Boot 3 Actuator Essentials

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon