0Pricing
MCP Academy · Lesson

Structured Logs You Can Search

Emit JSON logs with request correlation.

Structured Logs You Can Search is a free MCP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 📋

Frequently asked questions

Is the “Structured Logs You Can Search” lesson free?

Yes — the full text of “Structured Logs You Can Search” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Structured Logs You Can Search”?

Emit JSON logs with request correlation. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MCP Academy?

No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Structured Logs You Can Search” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MCP Academy lesson?

Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What to Measure in a Server
  2. Structured Logs You Can Search
  3. Trace a Tool Call End to End
  4. Alert on Failures & Abuse
← Back to MCP Academy