/blog

6 min read

Structured logs that answer the 3 a.m. question

When the pager goes off, you need to know what changed and who it hurt. Log fields that get you there in one query.

The question at 3 a.m. is never "what happened at 03:02:17". It is "which customers are affected and since when". Free-text logs can't answer it in one query; fields can.

The fields that earn their bytes

  • event: a stable name like charge.failed, never a sentence.
  • tenant_id and user_id, so you can count who is hurt.
  • version: the build that wrote the line. Most incidents start at a deploy.
  • duration_ms and status on every request.

One line, before and after

// before
log.Printf("failed to charge card for user %d: %v", uid, err)

// after
slog.Error("charge.failed",
    "user_id", uid, "tenant_id", tid,
    "version", buildVersion, "err", err)

Now "how many tenants since the last deploy" is a group-by, not an afternoon of grep.

$ subscribe

Want the next one?

Leave your email and new posts from Stack & Heap come to your inbox.

$ subscribe --email

Get new posts by email

Leave your address and new posts from Stack & Heap come to your inbox.

Rather write back? hello@stackandheap.example