In this post, we will explore different ways of setting the logging level in a Spring Boot application.

One of the important aspects of Spring Boot is that it supports various logging providers, such as Logback, Log4j2, and JUL (Java Util Logging). Logging is essential for debugging, monitoring, and auditing the application behavior and performance. However, to make the most of logging, we need to configure the logging level for different loggers.

The logging level is a threshold that determines which log messages are printed and which are ignored. The common logging levels are TRACE, DEBUG, INFO, WARN, ERROR, and FATAL, in ascending order of severity. For example, if we set the logging level to INFO, then only the log messages with INFO or higher level will be printed, and the log messages with TRACE or DEBUG level will be ignored.

1. Using application.properties or application.yml

The standard way of setting the logging level in a Spring Boot application is by using the application.properties or application.yml file. These files are the default configuration sources for Spring Boot, and they can be located in the classpath. In these files, we can define the logging level for different loggers using the following syntax:

 
The <logger-name> can be the name of any logger, such as the root logger, the package name, or the class name. The <level> can be one of the common logging levels, such as TRACE, DEBUG, INFO, WARN, ERROR, or FATAL. For example, we can set the logging level for the root logger to INFO, and the logging level for the com.example package to DEBUG, as follows:

The application.properties or application.yml file is useful when we want to set the logging level in a simple way. However, it cannot set the logging level dynamically at runtime.

2. Using Command Line Arguments

The command line arguments are useful when we want to set the logging level dynamically at runtime. These arguments are the parameters that we pass to the java command when we run the application. Spring Boot allows us to override any property defined in the application.properties or application.yml file by using the command line arguments with the following syntax:

The <application-jar> is the name of the executable JAR file that contains the application. The <property-name> is the name of any property defined in the application.properties or application.yml file. The <value> is the value that we want to assign to the property. For example, we can set the logging level for the root logger to DEBUG, and the logging level for the com.example package to TRACE, as follows:

That’s all about setting the logging level in a Spring Boot application.

 
Reference: Spring Boot features – Logging