検索できる構造化ログ
リクエストの相関情報を含むJSONログを出力します。
「検索できる構造化ログ」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 📋
よくある質問
「検索できる構造化ログ」レッスンは無料ですか?
はい。「検索できる構造化ログ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。
「検索できる構造化ログ」で何を学びますか?
リクエストの相関情報を含むJSONログを出力します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MCP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「検索できる構造化ログ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMCP Academyレッスンでコードを書いて実行できますか?
はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。