
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
- Visit https://start.spring.io/.
- Configure the project:
- Project: Maven
- Language: Java
- Spring Boot Version: 3.x.x
- Dependencies: Spring Web, Spring Boot DevTools, Spring Boot Logging
- 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:
- Configuring basic logging in Spring Boot using
application.properties
. - Using SLF4J to write structured logs.