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 app, 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

Let’s assume we have a Spring Boot Web 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 a console of your Spring Boot application a message, depending on the logging level activated.

To activate a specific logging level in my Spring Boot application at the runtime, I will need to pass a command-line argument and specify the logging level I would like 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 as well. For example, let’s run our app with a debug logging level set for a package in our app, as well as 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, that demonstrates how to startup a Spring Boot application with a specific logging level activated.

https://youtu.be/_Rqrq6VKpsY

I hope this short tutorial was helpful to you. If you are interested to learn more about Spring Boot and how to build RESTful Web Service applications, then I have many more Spring Boot video tutorials similar to this one available here: 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 *

Free Video Lessons

Enter your email and stay on top of things,

Subscribe!