Logging with SLF4J & Logback
Part of the Java Roadmap
In short
When a payment fails on a live server at 2 a.m., there is no debugger and no console. Logs are the only story of what happened, so writing good ones is a core professional skill.
SLF4J is a logging API and Logback is a common implementation (the default in Spring Boot). Logs with levels like DEBUG, INFO, WARN and ERROR replace System.out.println in real applications.

Think of it like this
The ship's logbook
A ship's captain writes the time, position and events in a logbook: routine notes, warnings about weather, and emergencies in red. If something goes wrong, investigators read the log. Your app's log works the same way, with levels to separate routine notes from emergencies.
Watch it happen
Log levels and the INFO threshold
1/4Five levels from most detailed to most serious.
The big ideas
What you need to know
API vs implementation
You code against SLF4J's Logger. Logback does the actual writing. Swapping the implementation needs no code changes.
Use {} placeholders
log.info("Order {} paid", id) only builds the string if INFO is enabled, and keeps logs consistent.
Configure, don't code
logback.xml (or application.properties in Spring Boot) sets levels, formats and whether logs go to console or files.
Never log secrets
No passwords, OTPs, card numbers or full Aadhaar numbers in logs. Log ids and mask the rest.
See it in code
Walk through the code
1import org.slf4j.Logger;2import org.slf4j.LoggerFactory;3 4public class PaymentService {5 private static final Logger log = LoggerFactory.getLogger(PaymentService.class);6 private final PaymentGateway gateway;7 8 public PaymentService(PaymentGateway gateway) {9 this.gateway = gateway;10 }11 12 public void pay(String orderId, long amountPaise) {13 log.info("Payment started for order {} amount {}", orderId, amountPaise);14 log.debug("Gateway timeout is {} ms", gateway.timeoutMillis());15 try {16 gateway.charge(orderId, amountPaise);17 log.info("Payment succeeded for order {}", orderId);18 } catch (GatewayException e) { // an unchecked exception19 log.error("Payment failed for order {}", orderId, e);20 throw e;21 }22 }23}1/4One static final logger per class, named after the class so you can tune its level separately.
Logging around a payment. Add the logback-classic dependency, which brings slf4j-api with it.
Save yourself the pain
Common mistakes
Oops
log.info("Paid " + orderId + " amount " + amount);
Do this instead
Use placeholders: log.info("Paid {} amount {}", orderId, amount). No string building when the level is off.
Oops
log.error("Failed: " + e.getMessage());
Do this instead
log.error("Failed for order {}", orderId, e) keeps the stack trace, which is what you need to fix it.
Oops
Leaving System.out.println calls in production code.
Do this instead
println has no levels, timestamps or files. Replace it with a logger.
Your mission
Add logging to a project
Quick check
The root level is set to WARN. Which call is written to the log?
Go deeper
- 1Replace println calls in a project with an SLF4J Logger.
- 2Use placeholders like log.info("User {} logged in", id) instead of string concatenation.
- 3Configure log levels and a file appender in logback.xml.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.