StoicBuilder

HandbookXII the SpeculaStructured logs

XIIthe Specula, Watch production

Structured logs

1 min readUpdated 28 November

In plain words

Structured logs write each event as named fields, usually one JSON object per line, so you can filter, count and join them across services instead of reading sentences by eye.

Where you see it

A customer says their Pro upgrade failed around 09:40. Instead of scrolling, you filter Harbor's logs for event=checkout.failed and userId=u_8812, get three lines, and follow their shared requestId into the billing service's lines for the same request.

services/billing/checkout.tsserver
log.warn({ event: "checkout.failed", requestId, userId: user.id, plan: "pro", stripeCode: err.code, // "card_declined"}, "checkout failed");
One event, named fields, and the same requestId in every service. Your log tool can now count card_declined per hour.

What goes wrong

  1. Sentences with the facts buried inside

    A line like this can only be found by a lucky text search, and never counted. Put the event, the user and the error code in their own fields.

    console.log(`User ${id} failed to upgrade: ${err.code}`);
  2. A different field name in every service

    Billing logs userId, the API logs user_id and the worker logs uid, so no single query finds one customer's trail. Agree on a short list of field names and use them everywhere.

  3. Logging everything at full volume, forever

    Debug lines on every request at real traffic can cost more than the servers. Keep debug for investigations, log info for events that matter, and set how long each kind is kept.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Replace console.log in the server code with a structured logger such as pino that writes one JSON object per line. Every line should carry event, requestId and userId where known, use the same field names in every service, and redact tokens, passwords and emails.

Quick check

Sign in to earn +10 XP for it

Harbor's billing logs say "Upgrade failed for user 8812 (card_declined)" as plain text. You want to know how many upgrades failed with card_declined in the last hour. What turns that into a one-line query?

Got a prompt that touches structured logs?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.