Agent Wranglers

STK-65Default Open source

Use JSON logs and Loki to store them

Write one JSON object per log line to standard output, and ship the lines to Loki or another store that searches by label.

Review by

2027-04-30

How to do it

JSON lines work with any store, so the format is the lasting choice and the store can change. Pino on npm reached 1.0 in March 2016 and has about 33 million weekly downloads. Structlog on PyPI is under MIT or Apache-2.0 and has about 25 million. Loki reached 1.0 in November 2019, is under AGPLv3, and 3.7.9 came out on 20 October 2026. Its image has over 4.8 billion pulls on Docker Hub. Loki indexes labels only, so it costs little to run next to Grafana. The fields to log are in Log structured events. Choose something else when you need full-text search over every field, and use OpenSearch.

Examples

JavaScript example

import pino from 'pino';

const log = pino({ base: { service: 'orders-api' } });
log.info({ orderId: 'ord_123', durationMs: 84 }, 'order placed');

Python example

import structlog

structlog.configure(processors=[structlog.processors.add_log_level,
                                structlog.processors.TimeStamper(fmt="iso"),
                                structlog.processors.JSONRenderer()])
structlog.get_logger().info("order placed", order_id="ord_123", duration_ms=84)

How to check

A script reads 100 lines of production log output, and every line parses as JSON with a time, level and message. A query in the store finds a test line within 60 seconds.

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/stack/operations-tools/json-logs-and-loki">Agent Wranglers software choices playbook</a>. In code you write, add a comment next to it with the same link.