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

    Production Logging

    Production logs for TechLearningPRO Orders are read by machines first: a JSON log shape, shipped to Loki, CloudWatch, or Datadog, queryable by traceId and orderId.

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

    Introduction

    Production logs for TechLearningPRO Orders are read by machines first: a JSON log shape, shipped to Loki, CloudWatch, or Datadog, queryable by traceId and orderId. Pretty plaintext is for local profiles only.

    On Boot 3, pair Logback (or Log4j2) with a JSON encoder, Micrometer Tracing MDC fields, and sane levels (INFO root; DEBUG only when diagnosing). Avoid logging payloads with PII or tokens.

    Correlate logs with metrics and traces so an on-call engineer jumps from a Grafana spike to the exact failing checkout span.

    This lesson hardens Orders logging for day-2 operations.

    Purpose of this lesson

    Configure structured JSON logging for Orders with trace correlation on Boot 3 / Java 21.

    Understanding the topic

    Logs are an observability signal, not printf debugging.

    • JSON in prod: stable fields for aggregators.
    • MDC: traceId, spanId, orderId on every line.
    • Levels: INFO default; warn/error for actionable events.
    • Shipping: stdout → collector → Loki/CloudWatch/Datadog.
    • Redaction: never log secrets, cards, or raw passwords.

    Step-by-step explanation

    1. Add a JSON encoder dependency (e.g. logstash-logback-encoder).
    2. Create logback-spring.xml with a prod profile JSON appender.
    3. Include service name, trace, and span from MDC in the pattern.
    4. Verify a checkout log line in your log backend filters by traceId.

    Informative example

    JSON logging for Orders prod profile.

    xml
    <!-- pom.xml dependency: net.logstash.logback:logstash-logback-encoder -->
    <!-- logback-spring.xml -->
    <configuration>
    <springProfile name="prod">
    <appender name="JSON" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
    <providers>
    <timestamp/>
    <logLevel/>
    <loggerName/>
    <message/>
    <stackTrace/>
    <mdc/>
    <pattern>
    <pattern>
    {"service":"${spring.application.name}","trace":"%mdc{traceId}","span":"%mdc{spanId}"}
    </pattern>
    </pattern>
    </providers>
    </encoder>
    </appender>
    <root level="INFO">
    <appender-ref ref="JSON"/>
    </root>
    </springProfile>
    <springProfile name="!prod">
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    </springProfile>
    </configuration>
    // In code — structured context, no secrets
    MDC.put("orderId", order.id().toString());
    log.info("order_placed tier={}", cmd.tier());

    Real-world use

    During a flash sale, TechLearningPRO on-call grepped plaintext logs for one order id across three services and missed the failure. After JSON + traceId in MDC, the same incident took a single Loki query — inventory returned 503, Orders logged order_place_failed with the shared trace.

    Security implications

    Logs are a sensitive data store.

    • Redact Authorization headers and payment fields.
    • Restrict who can query production log backends.
    • Never write secrets to logs 'temporarily' — they persist in retention windows.

    Best practices

    • One JSON schema shared across Orders, Pricing, Inventory.
    • Always attach trace ids when Micrometer Tracing is enabled.
    • Sample or drop high-volume health-check access logs.

    Common mistakes

    • DEBUG logging of full HTTP bodies in prod (PII + volume).
    • Inconsistent field names across services (trace_id vs traceId).
    • Logging exception messages that include SQL with bound sensitive parameters.

    Advanced interview questions

    Interview Prep

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

    3 questions
    1BeginnerQuestionWhy JSON logs in production?+

    Answer

    Machines parse stable fields for search and dashboards; plaintext forces brittle regex.
    2IntermediateQuestionHow do logs connect to distributed traces?+

    Answer

    Put traceId/spanId in MDC so every log line deep-links to the trace backend.
    3BeginnerQuestionWhat must never appear in Orders logs?+

    Answer

    Passwords, tokens, raw card data, and other secrets or highly sensitive PII.

    Hands-on exercise

    Lab. Enable JSON logging on Orders with prod profile. Place an order and query your log stack by traceId. Deliberately log a fake token, then add a redaction filter/unit test so it cannot regress.

    Summary

    Production logging makes TechLearningPRO Orders operable at scale. With containers, CI/CD, and cloud deploys in place, you can run Boot 3 services with confidence.

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