Setting the Log Level in Spring Boot When Testing
Last Updated :
15 May, 2024
When developing applications with Spring Boot, controlling log output is essential for diagnosing issues and understanding application behavior, especially during testing phases. Setting the log level allows developers to specify the type of information logged by the application, ranging from verbose output like debug information to critical errors only.
Prerequisites:
- Good Understanding of the Spring Boot and its configuration.
- Basic knowledge of the Spring Boot project step in your IDE.
- Familiarity with the logging frameworks used in the Spring Boot like SLF4J with logback.
Log Levels in Spring Boot
Logging levels provide a way to categorize and control the verbosity of the logs generated by the application. The common log levels in Spring Boot, which use SLF4J and Logback, include:
- ERROR: Logs that show error events which might still allow the application to continue running.
- WARN: Indicates potentially harmful situations that signal potential issues in the application's flow.
- INFO: A report message that displays the progress of an application at a coarse-grained level.
- DEBUG: Detailed information on the flow through the system, useful for development and debugging.
- TRACE: More detailed than DEBUG, logging the detailed trace of the message flows of the application.
Set Log levels in Spring Boot
We can set the log levels in Spring Boot through various methods like the application.properties or application.yml files. Here's how you can do it.
Using the application.properties:
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.com.yourpackage=INFO
Using the application.yml:
logging:
level:
root: WARN
org:
springframework:
web: DEBUG
com:
yourpackage: INFO
These configurations can control the logging level globally for all the Spring Web related logs and for the custom packages, respectively.
Project Implementation to Set the Log Level in Spring Boot When Testing
Below are the implementation steps to set the log level in spring boot application while testing.
Step 1:
Create a new Spring Boot project with the required dependencies using Spring Initializr.
Dependencies:
- Spring Web
- Spring DevTools
- Lombok
Once the project is created, the file structure will resemble the image below.

Step 2:
Open the application.properties file rename to application.yml and add the configuration for the logging of the root and package of the Spring application in the project.
spring:
application:
name: demo-logging
logging:
level:
root: WARN
org.example.springloggingdemo: INFO
Step 3: Create the Controller class.
We will create the GreetingController class that will create the REST API of the spring project. Go src > org.example.springloggingdemo > GreetingController and put the below code.
Java
package org.example.springloggingdemo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@GetMapping("/greeting")
public String greeting() {
return "Hello, World!";
}
}
Step 4: Main Class(No Changes are required)
Go src > org.example.springloggingdemo > SpringLoggingDemoApplication and put the below code.
Java
package org.example.springloggingdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringLoggingDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringLoggingDemoApplication.class, args);
}
}
Step 5: Create the Controller Test class.
We will create the GreetingControllerTest class that will create the test case of the spring project. Go src > test > org.example.springloggingdemo > GreetingController and put the below code.
Java
package org.example.springloggingdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
// Annotation to indicate that this is a Spring MVC test
@WebMvcTest
public class GreetingControllerTest {
// Autowired MockMvc instance for performing HTTP requests
@Autowired
private MockMvc mockMvc;
// Test method to verify the behavior of the greeting endpoint
@Test
public void testGreeting() throws Exception {
// Perform a GET request to the "/greeting" endpoint
mockMvc.perform(get("/greeting"))
// Expect HTTP status code 200 (OK)
.andExpect(status().isOk())
// Expect the response content to be "Hello, World!"
.andExpect(content().string("Hello, World!"));
}
}
Step 6: Main Test Class(No Changes are required)
Go src > test > org.example.springloggingdemo > SpringLoggingDemoApplicationTests and put the below code.
Java
package org.example.springloggingdemo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringLoggingDemoApplicationTests {
@Test
void contextLoads() {
}
}
Step 7:
Create the application-test.yml file and add the configuration of the testing package of the application.
logging:
level:
org.example.springloggingdemo: DEBUG
Step 8: Run the Application
Now, we will run the application then it will be start at port number 8080.

Step 9: Test the Application
Endpoint API:
http://localhost:8080/greeting
Output:

Similar Reads
Spring Boot - Getting Started
Spring Boot is a part of the larger Spring Framework ecosystem which is known for its comprehensive programming and configuration model for the modern Java-based enterprise applications. Spring Boot has emerged as a go-to framework for creating REST APIs, microservices, and web applications with les
6 min read
Spring Boot MockMVC Testing with Example Project
In a Spring Boot project, we have to test the web layer. For that, we can use MockMVC. In this tutorial, let us see how to do that by having a sample GeekEmployee bean and writing the business logic as well as the test cases for it. Example Project Project Structure: This is a maven project. Let's s
5 min read
Testing in Spring Boot
In this article, we will be discussing Testing in Spring Boot. There are so many different testing approaches in Spring Boot used for deploying the application server. Testing in Spring Boot is an important feature of software development which ensures that your application behaves as per the requir
4 min read
Returning Errors Using ProblemDetail in Spring Boot
In a Spring Boot application, error handling is an important aspect of providing a robust and user-friendly API. Traditionally, Spring Boot has provided various methods like @ExceptionHandler, ResponseEntityExceptionHandler, and @ControllerAdvice for handling exceptions. With Spring Framework 6 and
7 min read
Unit Testing in Spring Boot Project using Mockito and Junit
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
6 min read
Loading Initial Data with Spring Boot
Loading initial data into a Spring Boot application is a common requirement for seeding the database with predefined data. This data could include reference data, default settings, or simple records to populate the application upon startup. The main concept involves using Spring Boot's data initiali
3 min read
How to Set the Logging Level with application.properties?
In Spring Boot, logging is crucial for monitoring, debugging, and troubleshooting Java applications. Spring Boot provides a convenient way to configure logging levels using properties files, particularly the application.properties file of the Spring application. This allows developers to control the
5 min read
What is PathVariable in the Spring Boot?
Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using JAVA is that Java tries to connect every conc
3 min read
How to Test Spring Boot Project using ZeroCode?
Zerocode automated testing framework for a REST API project concept is getting seen via this tutorial by taking a sample spring boot maven project. Let us see the dependencies for Zerocode : org.jsmart zerocode-tdd 1.3.27 test Zerocode framework supports the following RESTSOAPSecurityLoad/StressData
4 min read
Testing Spring Security Auth with JUnit
Here we are going to learn how to use InMemoryDaoImpl to verify Spring security authentication using a JUnit test case and how to programmatically create a fully complete authentication object and then utilize it in an application. SecurityContextHolder: Spring security is built on the concept of a
4 min read