Topic 10: – 5 steps of Spring Boot Logging

image 2

Play Store Application link – Spring Boot in 13 steps – App on Google Play


Github project link – https://github.com/kuldeep101990/Spring-Boot-Logging

Step 1: Setting Up a Spring Boot Project

Using Spring Initializr

  1. Visit https://start.spring.io/.
  2. Configure the project:
    • Project: Maven
    • Language: Java
    • Spring Boot Version: 3.x.x
    • Dependencies: Spring Web, Spring Boot DevTools, Spring Boot Logging
  3. Click Generate to download the project ZIP and extract it.

Import into IDE

  • Import the project into your favorite IDE (IntelliJ, Eclipse, etc.) as a Maven project.

Step 2: Basic Logging Configuration

Default Logging Configuration

Spring Boot uses Logback by default for logging. To configure logging levels, modify application.properties:

logging.level.root=INFO
logging.level.org.springframework.web=DEBUG
logging.level.com.example=ERROR


Step 3: Add Logging to Your Application

Main Application Class with Logging

package com.example.loggingdemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LoggingDemoApplication {
    private static final Logger logger = LoggerFactory.getLogger(LoggingDemoApplication.class);
    public static void main(String[] args) {
        SpringApplication.run(LoggingDemoApplication.class, args);
        logger.info("Application started successfully.");
    }
}


Step 4: Using SLF4J for Structured Logs

Service Class with Logs

package com.example.loggingdemo.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class CalculationService {
    private static final Logger logger = LoggerFactory.getLogger(CalculationService.class);
    public int calculateSquare(int number) {
        logger.debug("Calculating square for number: {}", number);
        int result = number * number;
        logger.info("Square of {} is {}", number, result);
        return result;
    }
}

Controller Class to Access Service

package com.example.loggingdemo.controller;
import com.example.loggingdemo.service.CalculationService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CalculationController {
    private final CalculationService calculationService;
    public CalculationController(CalculationService calculationService) {
        this.calculationService = calculationService;
    }
    @GetMapping("/square")
    public int getSquare(@RequestParam int number) {
        return calculationService.calculateSquare(number);
    }
}


Step 5: Configure Logback (Optional)

Customize logback-spring.xml

Create or modify the logback-spring.xml file in src/main/resources:

<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} - %level - %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="console"/>
    </root>
</configuration>

This will log in the format 2025-01-07 12:00:00 - INFO - com.example.loggingdemo - Application started successfully.


Conclusion

In this guide, we focused on:

  1. Configuring basic logging in Spring Boot using application.properties.
  2. Using SLF4J to write structured logs.

Leave a Reply

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