Przeszukiwalne logi strukturalne
Wysyłaj logi JSON z korelacją żądań.
Przeszukiwalne logi strukturalne to bezpłatna lekcja MCP Academy na CoddyKit. To lekcja 2 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 MCP Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs MCP Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Logs You Can Actually Use
Plain text logs read fine to a human but fight you at scale. Structured logs store each event as data, so machines can filter and search them fast. 🔎
Why JSON Wins
Emit each log line as a JSON object. Every field becomes searchable, so you can query by tool, status, or duration without fragile text parsing.
Anatomy of a Log Event
A good event carries a timestamp, a level, a message, and context fields. Together they tell you what happened, when, and why.
{"ts": "2026-01-01T10:00:00Z",
"level": "info",
"msg": "tool called",
"tool": "search"}Pick Sensible Levels
Use log levels with intent: debug for detail, info for normal flow, warning for trouble, error for failures. Levels let you turn noise up or down.
The Correlation ID
Give each request a unique correlation ID and stamp it on every log it produces. Now one search pulls the full story of a single call.
Generate an ID
A UUID makes a fine correlation ID: it is unique enough to never collide across requests.
import uuid
request_id = str(uuid.uuid4())
print(request_id)Log to stderr, Not stdout
Over stdio, stdout carries the MCP protocol itself. Always send logs to stderr so they never corrupt the JSON-RPC messages.
Python's logging Module
Python's built-in logging module gives you levels and handlers for free. Point a handler at stderr and you are halfway there.
import logging, sys
logging.basicConfig(stream=sys.stderr,
level=logging.INFO)
log = logging.getLogger("mcp")Add Fields, Not Strings
Resist stuffing values into the message text. Pass them as extra fields so each one stays its own searchable column later.
log.info("tool finished",
extra={"tool": "search",
"ms": 42})Never Log Secrets
Logs travel and persist, so redact tokens, keys, and personal data before writing them. A leaked secret in a log is a real breach. 🔒
Keep One Line Per Event
Write one JSON object per line. This line-delimited format is what log shippers and search tools expect to ingest cleanly.
Quick Check
One detail trips up many MCP authors.
Recap
You learned to log as searchable JSON: one event per line, useful levels, a correlation ID, redacted secrets, and output on stderr. 📋
Często zadawane pytania
Czy lekcja „Przeszukiwalne logi strukturalne” jest bezpłatna?
Tak — pełny tekst „Przeszukiwalne logi strukturalne” 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 MCP Academy, przejdź na CoddyKit PRO. Kurs MCP Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Przeszukiwalne logi strukturalne”?
Wysyłaj logi JSON z korelacją żądań. Ćwiczysz MCP Academy 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ąć MCP Academy?
Nie wymagamy żadnego doświadczenia. MCP Academy 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 2 z 4.
Ile czasu zajmuje lekcja „Przeszukiwalne logi strukturalne”?
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 MCP Academy?
Tak. Każda lekcja MCP Academy 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
- Co mierzyć na serwerze
- Przeszukiwalne logi strukturalne
- Śledzenie wywołania narzędzia od początku do końca
- Alarmy o awariach i nadużyciach