0 day streak
java / beginner
Snippet

Application Logging with SLF4J

Spring Boot uses SLF4J as a logging abstraction. It is best practice to log important events instead of using System.out.println for debugging in production.

snippet.java
java
1
2
3
4
5
6
7
8
public class MyService {
private static final Logger logger = LoggerFactory.getLogger(MyService.class);
public void process() {
logger.info("Starting process...");
logger.warn("Observation made during execution.");
}
}
spring
Breakdown
1
LoggerFactory.getLogger(MyService.class)
Creates a logger instance specifically for this class.
2
logger.info("...")
Logs an informational message to the console or file.
3
logger.warn("...")
Logs a warning that something unexpected but non-fatal happened.