Logs und Traces korrelieren
Verknüpfen Sie Logs mit dem Trace-Kontext.
Logs und Traces korrelieren ist eine kostenlose Spring Boot 4 Microservices & REST APIs-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Boot 4 Microservices & REST APIs-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Linking logs to traces
Traces show the request flow; logs show the details. Putting the traceId into every log line lets you jump from a trace to its exact logs - and vice versa.
What is MDC?
MDC (Mapped Diagnostic Context) is a per-thread key/value map provided by SLF4J/Logback. Values placed in it can be printed in every log line by the layout pattern.
Micrometer populates the MDC
Micrometer Tracing automatically puts the current traceId and spanId into the MDC, so they are available to the logging framework without manual code.
Adding IDs to the log pattern
Reference the MDC keys in your Logback pattern so each line carries the IDs.
# logback-spring.xml pattern
# %d{ISO8601} [%X{traceId:-},%X{spanId:-}] %-5level %logger - %msg%nSpring Boot default pattern
Spring Boot can inject the IDs for you. Setting the application name enables the default correlation pattern that prepends [app,traceId,spanId] to logs.
spring:
application:
name: order-service
# logs become: [order-service,4bf9...,00f0...] INFO ...A correlated log line
The result looks like this - the bracketed IDs tie the line to a specific trace and span.
// 2026-05-30 10:00:00 [order-service,4bf92f35...,00f067aa...] INFO c.a.OrderService - Order placedSearching by traceId
With IDs in logs, your log aggregator (ELK, Loki, etc.) can filter by traceId. Copy an id from Zipkin, paste it into the log search, and see every related log line.
Custom baggage in logs
You can also surface baggage (e.g. tenant id) in logs by mapping a baggage field into the MDC, enriching every line with business context.
management:
tracing:
baggage:
correlation:
fields: tenantIdTwo-way navigation
The goal is bidirectional: from a slow span in Zipkin -> open its logs by traceId; from an error log -> open its trace in Zipkin. This dramatically speeds up debugging.
Keep IDs out of business logs by accident
Because IDs come from MDC, they appear only when a trace is active. Background threads without a trace context show empty IDs (%X{traceId:-} renders blank) - which is expected.
Structured logging
For best results emit logs as JSON with traceId/spanId as proper fields, so log tools can index and link them reliably instead of parsing text.
Quick Check
Test your correlation knowledge.
Recap
You correlated logs and traces:
- Micrometer puts
traceId/spanIdinto the MDC - Reference them in the Logback pattern (or use Boot's default)
- Search logs by traceId to jump between traces and logs
- Prefer structured JSON logs for reliable indexing
Häufig gestellte Fragen
Ist die Lektion „Logs und Traces korrelieren“ kostenlos?
Ja — der vollständige Text von „Logs und Traces korrelieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Boot 4 Microservices & REST APIs-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Logs und Traces korrelieren“?
Verknüpfen Sie Logs mit dem Trace-Kontext. Du übst Spring Boot 4 Microservices & REST APIs mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Spring Boot 4 Microservices & REST APIs zu starten?
Keine Vorkenntnisse erforderlich. Spring Boot 4 Microservices & REST APIs auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Logs und Traces korrelieren“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Spring Boot 4 Microservices & REST APIs-Lektion Code schreiben und ausführen?
Ja. Jede Spring Boot 4 Microservices & REST APIs-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Warum verteiltes Tracing wichtig ist
- Micrometer Tracing
- Traces nach Zipkin exportieren
- Logs und Traces korrelieren