Spring Boot 4 Microservices & REST APIs · Lekcja

Powiązywanie logów i śladów

Łącz logi z kontekstem śladu

Lekcja 4 z 413 kroki

Powiązywanie logów i śladów to bezpłatna lekcja Spring Boot 4 Microservices & REST APIs na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Spring Boot 4 Microservices & REST APIs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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%n

Spring 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 placed

Searching 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: tenantId

Two-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/spanId into 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
Bezpłatny start

Ucz się Java dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
24
Lekcje
93

Często zadawane pytania

Czy lekcja „Powiązywanie logów i śladów” jest bezpłatna?

Tak — pełny tekst „Powiązywanie logów i śladów” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Spring Boot 4 Microservices & REST APIs, przejdź na CoddyKit PRO. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.

Co nauczysz się w „Powiązywanie logów i śladów”?

Łącz logi z kontekstem śladu Ćwiczysz Spring Boot 4 Microservices & REST APIs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Spring Boot 4 Microservices & REST APIs?

Nie wymagamy żadnego doświadczenia. Spring Boot 4 Microservices & REST APIs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Powiązywanie logów i śladów”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Spring Boot 4 Microservices & REST APIs?

Tak. Każda lekcja Spring Boot 4 Microservices & REST APIs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Dlaczego śledzenie rozproszone ma znaczenie
  2. Śledzenie za pomocą Micrometer
  3. Eksportowanie śladów do Zipkin
  4. Powiązywanie logów i śladów
← Powrót do Spring Boot 4 Microservices & REST APIs