0Pricing
MLOps Academy · Lección

Logs estructurados para predicciones

Registre entradas, salidas y latencia en formato JSON.

Logs estructurados para predicciones es una lección gratuita de MLOps Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de MLOps Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de MLOps Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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

Preguntas frecuentes

¿La lección «Logs estructurados para predicciones» es gratis?

Sí — el texto completo de «Logs estructurados para predicciones» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de MLOps Academy, actualiza a CoddyKit PRO. El curso de MLOps Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Logs estructurados para predicciones»?

Registre entradas, salidas y latencia en formato JSON. Practicas MLOps Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar MLOps Academy?

No se requiere experiencia previa. MLOps Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.

¿Cuánto tiempo toma la lección «Logs estructurados para predicciones»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de MLOps Academy?

Sí. Cada lección de MLOps Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Logs estructurados para predicciones
  2. Exponga métricas con Prometheus
  3. Cree un dashboard de Grafana
  4. Alerta ante picos de latencia y errores
← Volver a MLOps Academy