Debugging & Logging
Prerequisites: Unit testing with JUnit
A real application logs instead of using System.out.println —
logging can be turned on/off or filtered by severity without editing
code, and includes a timestamp and source location automatically.
import java.util.logging.Logger;
class OrderService {
private static final Logger log = Logger.getLogger(OrderService.class.getName());
void placeOrder(String id) {
log.info("Placing order " + id);
}
}
In real projects, SLF4J with Logback is far more common than
the built-in java.util.logging shown above, but the core idea —
log levels (INFO, WARN, ERROR) instead of always printing —
is the same either way.