Why this matters
- Grepping unstructured log files across twelve pods during an outage is slow; JSON logs with a correlation ID let you filter one checkout journey in seconds.
- Micrometer meters are vendor-neutral — the same
Timerfeeds Prometheus, Datadog, or CloudWatch depending on which registry you add at deploy time. - Business metrics like
orders.placedandinventory.reservations.failedbelong alongside JVM metrics; they surface revenue-impacting problems before users complain.
SLF4J and Logback
L1 · FresherLog with Logger interfaces; Boot configures Logback with sensible defaults and correlation fields.
Structured logging
L2 · IntermediateJSON logs with traceId in MDC make grep-free searching in ELK or CloudWatch.
Micrometer metrics
L3 · Advanced@Timed and MeterRegistry counters expose HTTP latency and business KPIs to Prometheus.
Cardinality control
L4 · ExpertAvoid high-cardinality tags (user IDs) on metrics — they explode storage cost and slow queries.
Logs
Logback + MDCStructured JSON
Metrics
MicrometerCounters, timers
Traces
OpenTelemetryTrace propagation
Logging and metrics toolkit
- MDC (Mapped Diagnostic Context) — thread-local map of key-value pairs appended to every log line in that thread.
- Logback
logback-spring.xml— configure JSON appenders, log levels per package, and profile-specific overrides. MeterRegistry— central registry for counters, timers, gauges, and distribution summaries.@Timed/@Counted— declarative instrumentation via Micrometer's aspect (requires@EnableAspectJAutoProxy).ObservationAPI — unified abstraction for metrics, tracing, and logging context in Spring Boot 3.x.
Correlation IDs with MDC
// learn:fragment
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorrelationIdFilter extends OncePerRequestFilter {
public static final String HEADER = "X-Correlation-Id";
public static final String MDC_KEY = "correlationId";
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain chain) throws ServletException, IOException {
String correlationId = Optional.ofNullable(request.getHeader(HEADER))
.filter(id -> !id.isBlank())
.orElse(UUID.randomUUID().toString());
MDC.put(MDC_KEY, correlationId);
response.setHeader(HEADER, correlationId);
try {
chain.doFilter(request, response);
} finally {
MDC.remove(MDC_KEY);
}
}
}
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 1 section
- Custom business metrics
Test yourself
Answer these before moving on — recall is what makes it stick.