0Pricing
MLOps Academy · Lesson

Structured Logs for Predictions

Log inputs, outputs, and latency as JSON.

Structured Logs for Predictions is a free MLOps Academy lesson on CoddyKit — lesson 1 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 MLOps Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Log Predictions

Once your model serves real users, you cannot watch it by hand. Logging every prediction is how you see what it actually does in production. 📊

Plain Text Is Painful

A log line like "got request, returned 0.91" reads fine to you but is brutal to search at scale. Machines need structure, not prose.

Structured Logging

Structured logging means each log line is a small object of named fields, not a sentence. Tools can then filter, group, and aggregate them instantly.

JSON Is the Format

The common choice is one JSON object per line. Every field has a key, so log systems parse it the same way every time.

{"event": "prediction", "model": "churn-v3", "score": 0.91}

What to Log per Prediction

For each call, capture the inputs, the output, and how long it took. Together these let you debug, audit, and measure quality later.

Log the Latency

Always record latency in milliseconds. It is your earliest warning that a model or its server is starting to struggle.

import time
start = time.perf_counter()
score = model.predict(x)
latency_ms = (time.perf_counter() - start) * 1000

Add an ID to Trace It

Give every request a unique request_id. Later you can follow one prediction across logs, dashboards, and the eventual real outcome.

import uuid
request_id = str(uuid.uuid4())

Build the Log Record

Collect your fields into one dictionary. This single record becomes one searchable JSON line in your logs.

record = {
    "request_id": request_id,
    "model": "churn-v3",
    "score": score,
    "latency_ms": latency_ms,
}

Emit JSON with the Logger

Use Python's built-in logging module and dump the record as JSON. Never use print, which skips levels and timestamps.

import json, logging
log = logging.getLogger("predictions")
log.info(json.dumps(record))

Never Log Raw Secrets

Be careful what goes in. Skip passwords, tokens, and raw PII, or hash sensitive fields before they ever touch a log line. 🔒

Ship Logs Somewhere Central

Logs on one server vanish when it restarts. Forward them to a central store like Loki or CloudWatch so they outlive any single container.

Quick Check

You want prediction logs you can search and aggregate at scale. What format fits best?

Recap

You log every prediction as a structured JSON record with inputs, output, latency, and an id, then ship it somewhere central. That is your eyes in production. ✅

Frequently asked questions

Is the “Structured Logs for Predictions” lesson free?

Yes — the full text of “Structured Logs for Predictions” is free to read here on the web, and the MLOps 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 MLOps Academy course, upgrade to CoddyKit PRO.

What will I learn in “Structured Logs for Predictions”?

Log inputs, outputs, and latency as JSON. You practise MLOps 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 MLOps Academy?

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

How long does the “Structured Logs for Predictions” 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 MLOps Academy lesson?

Yes. Every MLOps 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. Structured Logs for Predictions
  2. Expose Metrics with Prometheus
  3. Build a Grafana Dashboard
  4. Alert on Latency and Error Spikes
← Back to MLOps Academy