Open In App

Setting the Log Level in Spring Boot When Testing

Last Updated : 15 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Folder Structure

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.

Application Runs


Step 9: Test the Application

Endpoint API:

http://localhost:8080/greeting

Output:

Browser Output



Next Article

Similar Reads