Skip to content

Lesson 13 of 14 · 15m

Logs You Can Grep at 3 a.m.

pino JSON to stdout, a request ID on every line, and redaction at the logger turn 3 a.m. incidents into answers instead of dead ends.

A log line at 3 a.m. is either an answer or a dead end, and which one it is was decided the day you wrote it. Something went wrong is a dead end. {"level":30,"reqId":"f47ac10b","route":"POST /orders","durMs":212,"status":201} is an answer. Structured logging — JSON you can jq, index, and alert on — is the whole difference. Prose is for humans reading one line; a service emits thousands per second for machines to filter.

pino, stdout, levels

pino is the Node default because it was built for throughput — line-delimited JSON at a fraction of the cost the older loggers charged. Log to stdout, always: the platform — Docker, the systemd journal, your collector — picks it up there. A service writing /var/log/app.log inside a container is invisible at exactly the moment the container is gone. pino-pretty and transports are dev toys or off-thread workers — never a blocking pretty-print in the request path. pino-http (or Hono's logger()) stamps the request log automatically, and pino.destination({ sync: false }) buffers writes for throughput at the cost of needing a flush before exit — a hard crash drops the tail. Levels are a contract: info means an event happened, warn means recoverable-but-wrong, error means a person must act. If everything is error, nothing is.

Log the err object, not err.message — pino serializes the stack and cause chain; a message string is where "something went wrong" comes from. And volume is a bill: sample debug/info on hot paths or your observability costs more than the API it watches.

The request id is the spine

app.use(async (c, next) => {
  const id = c.req.header("x-request-id") ?? crypto.randomUUID();
  c.set("reqId", id);
  await next();
  c.res.headers.set("x-request-id", id);
});

One ID per request, stamped on every log line inside it and forwarded on every outbound call — that is what turns forty scattered lines into one story. Echo it into your error bodies too: "quote reqId f47ac10b" is a debugging session a user can actually initiate. AsyncLocalStorage (node:async_hooks) propagates it through awaits without threading a parameter through forty functions. The distributed version already has a standard: the W3C traceparent header carries trace/span IDs across service boundaries — honor it inbound, emit it outbound.

Redaction is a security feature

A leaked log is a credential dump: authorization headers, cookies, request bodies carrying password. Twitter disclosed in 2018 that unmasked passwords had been written to an internal log — the mechanism is mundane: somebody logs req.body or a whole request object and the secrets ride along. pino's redact option — redact: ["req.headers.authorization", "*.password"] — exists for this; set it on the logger, not by developer discipline.

One more split worth making: application logs answer "what did the code do"; audit logs answer "who did what" — login, permission change, export — and belong in a durable store with different retention, not stdout's stream. And console.log in a request handler is not a logger: it writes synchronously-ish, carries no level, no reqId, no redaction. Fine for scripts; a bug in a service.

Metrics and the signals that matter

Logs are events; metrics are aggregates. A /metrics endpoint for Prometheus — counters for requests and errors, histograms for latency, gauges for pool depth — answers "is it getting worse" in a way grep can't. RED is the shorthand every route needs — rate, errors, duration — and prom-client exposes the series to a /metrics scrape in a few lines. The four golden signals: latency, traffic, errors, saturation. Cardinality is the metric-side footgun: a label per user-id is a time-series explosion, so labels are route names and statuses, never identifiers. OpenTelemetry stitches the distributed version — one trace across edge → API → database, with your request id as the trace's public face.

// Runnable: what a structured line looks like
console.log(JSON.stringify({ level: 30, reqId: "f47ac10b", route: "POST /orders", durMs: 212, status: 201 }));

The practice

JSON logs to stdout; a request ID on every line and every outbound call; redact at the logger; error means someone acts; metrics for trends, logs for stories, traces for spans.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.