Logging

Basic Spring
≈ 1 min read

Logs instead of System.out.println

A running application should leave a clear diagnostic trail: what started, which operation was running, and where an error occurred. Spring Boot prepares the logging system, so the application only needs to write messages through the standard logging API instead of scattering System.out.println() calls throughout the code.

class PaymentService {
    private static final Logger log = LoggerFactory.getLogger(PaymentService.class);

    void pay(long orderId) {
        log.info("Starting payment for order {}", orderId);
    }
}

The {} placeholder lets you pass a value separately from the message template. This is more convenient than string concatenation and works naturally with logging levels.

Levels separate important events from details

Messages use levels such as DEBUG, INFO, WARN, and ERROR. The required level of detail can be changed through configuration without rewriting Java code.

logging.level.com.example.payment=DEBUG
Do not log secrets or personal data unless it is truly necessary. Logs are often collected centrally and kept longer than a single request, so an accidentally recorded token or password becomes a separate security problem.