Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 23

    Logging Basics

    Orders uses SLF4J with Logback by default in Spring Boot.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Orders uses SLF4J with Logback by default in Spring Boot. Inject nothing exotic: private static final Logger log = LoggerFactory.getLogger(OrderService.class);

    Log at the right level: INFO for business milestones (order placed), WARN for recoverable problems, ERROR for failures needing action, DEBUG for local diagnosis. Avoid logging every GET at INFO in production.

    Prefer parameterized messages: log.info("placed order id={}", id) — not string concatenation. Never log tokens, passwords, or full card-like payloads.

    Configure levels via logging.level.com.techlearningpro.orders=INFO in YAML profiles.

    Purpose of this lesson

    Add safe, structured-enough logging to Orders services and configure levels per environment.

    Understanding the topic

    Logs are an operations API.

    • SLF4J API / Logback impl: Boot's default pairing.
    • Levels: ERROR, WARN, INFO, DEBUG, TRACE.
    • Parameterized messages: defer formatting and guard work.
    • MDC: correlation ids for a request (optional next step).
    • Config: application.yml and env-specific overrides.

    Step-by-step explanation

    1. Add a logger to OrderService.
    2. Log id and sku on place at INFO — not the entire request body.
    3. Set DEBUG locally and INFO in prod profile.
    4. Trigger an error path and confirm ERROR includes a stack trace in logs only.

    Informative example

    Service logging.

    java
    @Service
    public class OrderService {
    private static final Logger log = LoggerFactory.getLogger(OrderService.class);
    // ...
    public OrderView place(CreateOrderRequest req) {
    OrderView created = /* ... */;
    log.info("order_placed id={} sku={} qty={}", created.id(), req.sku(), req.qty());
    return created;
    }
    }

    YAML level pin:

    yaml
    logging:
    level:
    com.techlearningpro.orders: INFO
    org.hibernate.SQL: WARN

    Real-world use

    On-call engineers find checkout incidents by searching order_placed and correlation ids. TechLearningPRO Orders should make those events obvious without turning log sinks into a PII warehouse.

    Security implications

    Logs are sensitive storage.

    • Redact secrets and personal data.
    • Restrict who can read production log streams.
    • Do not log full payment payloads.

    Best practices

    • Stable event-like messages (order_placed) for grep/metrics.
    • Log exception objects as the last SLF4J argument to capture stacks.
    • Tune third-party loggers (Hibernate, security) separately.

    Common mistakes

    • Logging passwords or bearer tokens.
    • INFO spam on high-volume GETs.
    • System.out.println in services.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat logging facade does Spring Boot apps typically use?+

    Answer

    SLF4J, commonly backed by Logback.
    2IntermediateQuestionWhy use parameterized log messages?+

    Answer

    They avoid unnecessary string work when the level is disabled and keep formatting consistent.
    3IntermediateQuestionWhat must never appear in Orders logs?+

    Answer

    Secrets, session tokens, and sensitive personal payloads — log identifiers instead.

    Hands-on exercise

    Lab. Add order_placed INFO logging. Set package DEBUG locally, then switch prod profile to INFO and confirm debug lines disappear.

    Summary

    Orders logging makes operations possible without leaking data. Next you connect the service to a real database.

    Ready to mark this lesson complete?Track your journey across the entire course.