Switch Logging Level in Spring Boot at Runtime

In this short tutorial, I am going to share with you how to switch your Spring Boot application, at runtime, to use a different logging level.

For video lessons on how to secure your Spring Boot application with OAuth 2.0. and Spring Security 5, please checkout my complete video course OAuth 2.0. in Spring Boot applications.

Set Logging Level for a Package in Your App

Suppose we have a Spring Boot Application with the following method in a Rest Controller class.

@RestController
@RequestMapping("/users")
public class UsersController {

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

	@DeleteMapping(path = "/{id}")
	public String deleteUser(@PathVariable String id) {
		logger.info("Debug level message: Deleting user with id: " + id);

		logger.error("Error level is On");
		logger.warn("Warn level is On");
		logger.info("Info level is On");
		logger.debug("Debug level is On");
		logger.trace("Trace level is On");

		return "HTTP DELETE request was sent";
	}

}

The above method will log into your Spring Boot application console a message, depending on the logging level activated.

To activate a specific logging level in my Spring Boot application at runtime, I will need to pass a command-line argument and specify the logging level I want to set.

mvn spring-boot:run -Dspring-boot.run.arguments=--logging.level.<logger>=<logging-level>

where

  • logger – is a package name for which you would like to set the logging level,
  • logging-level – is one of the logging levels. For example, as a logging-level value, you can use one of the following: trace, warn, error, info, or debug.

For example, to start my application that has a root package name com.appsdeveloperblog with a trace logging level activated, I will use the following command.

mvn spring-boot:run -Dspring-boot.run.arguments=--logging.level.com.appsdeveloperblog=trace

To make my app use debug logging level, I will start it up with the following command-line argument.

mvn spring-boot:run -Dspring-boot.run.arguments=--logging.level.com.appsdeveloperblog=debug

Logging Level for the Spring Framework Package

You can also set the logging level for the Spring Framework package. For example, let’s run our app with a debug logging level set for a package in our app and a Spring Framework package “org.springframework”.

mvn spring-boot:run -Dspring-boot.run.arguments="--logging.level.com.appsdeveloperblog=debug --logging.level.org.springframework=debug"

Just make sure to replace the “com.appsdeveloperblog” package name I used in the examples below with the root package name of your Spring Boot Web application.

Video Tutorial

Below is a video tutorial demonstrating how to start a Spring Boot application and switch logging level at runtime.

I hope this short tutorial was helpful to you.

If you want to learn more about Spring Boot and how to build RESTful Web Service applications, read the following tutorials RESTful Web Services with Spring Boot and Spring MVC.

For video lessons on how to secure your Spring Boot application with OAuth 2.0. and Spring Security 5, please checkout my complete video course OAuth 2.0. in Spring Boot applications.


Leave a Reply

Your email address will not be published. Required fields are marked *