Agent Wranglers

OPS-6Recommended

Log structured events

Write each log entry as one line of structured data, such as JSON, with a time, a level, an event name and its fields.

How to do it

Use a logging library that writes JSON, such as pino, structlog or Monolog. Write the time in UTC as ISO 8601 with milliseconds, and keep server clocks synced with NTP. Name events with fixed words, like order.paid, and put values in fields, not in the message text. Write to standard output and let the platform collect it, as the Twelve-Factor App advises. The short version for any project, with error tracking and alerts, is Log, track errors and alert.

Examples

JavaScript example

import pino from 'pino';

const log = pino({
  timestamp: pino.stdTimeFunctions.isoTime,
  formatters: { level: label => ({ level: label }) },
  messageKey: 'event',
});
log.warn({ request_id: '7f3c2a9e', order: 'A-1042', attempt: 2 }, 'payment.retry');

Python example

import structlog

structlog.configure(processors=[
    structlog.contextvars.merge_contextvars,
    structlog.processors.add_log_level,
    structlog.processors.TimeStamper(fmt="iso", utc=True, key="time"),
    structlog.processors.JSONRenderer(),
])
structlog.get_logger().warning("payment.retry", request_id="7f3c2a9e", order="A-1042", attempt=2)

JSON example

{"time":"2026-11-02T09:15:04.120Z","level":"warn","event":"payment.retry","request_id":"7f3c2a9e","order":"A-1042","attempt":2}

Shell example

journalctl -u shop --since -1h -o cat | jq -s -e 'all(.time and .level and .event)' > /dev/null && echo PASS

How to check

Pipe one hour of logs through a JSON parser, such as jq. Every line parses, and every line has a time, a level and an event.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/operations/logs/structured-logs">Agent Wranglers operations and observability playbook</a>. In code you write, add a comment next to it with the same link.