Skip to content
tezvyn:
Menu

Source-Concept Mismatch: Structured Logging

Source: thebigclass.comMediumHow cards are made

Source-Concept Mismatch: Structured Logging

Structured logging replaces free-text console.log output with consistently shaped JSON log lines carrying fields like timestamp, level, and request id, so aggregators can filter and correlate logs by machine, not by a human grepping text.

Why it exists

Plain console.log output is a free-text sentence meant for a human reading a single terminal in real time. Once an app runs as many concurrent instances across containers, that terminal no longer exists, and finding one request's logs among millions of interleaved lines by string matching becomes impractical. Structured logging exists to make log lines machine-parseable by default, so searching, filtering, and alerting can run as queries over fields instead of text search over prose.

The mental model

Structured logging is filling out a form instead of writing a sentence. A free-text log spells out, as one sentence, that user 4021 failed checkout because of a Stripe timeout. A structured log says the same thing as named fields: event, level, userId, provider, reason. A form is slower to write by hand but trivial for software to search, sort, and aggregate, which is exactly the tradeoff that matters once logs are read by machines first and humans second.

How it works

A logging library like pino or winston replaces console.log calls with a logger that emits newline-delimited JSON. Express middleware, such as pino-http, generates or reads a correlation id, often from an x-request-id header, and attaches a child logger to the request carrying that id, so every log statement made anywhere while handling that request automatically includes it without threading the id through every function call by hand. Log levels, debug, info, warn, error, let production filter volume without a redeploy. The JSON stream ships to an aggregator like Datadog, the ELK stack, or CloudWatch, which indexes each field so a query for one request id returns that request's entire lifecycle in order.

When it matters

It matters the moment an app runs more than one instance or a production incident needs reconstructing after the fact. The footgun is logging sensitive fields, passwords, tokens, full request bodies, as structured data, which makes them trivially searchable and exportable in the aggregator, often a worse exposure than the same value buried in an unstructured string. Over-logging at info level in a hot path also adds real serialization and shipping cost.

A concrete example

An Express checkout endpoint assigns each request an id via pino-http. When a customer's payment fails, filtering the aggregator by that one request id instantly shows auth passing at 10:02:01, the cart validated at 10:02:02, then a Stripe timeout logged at level error, provider stripe, at 10:02:03, rather than grepping thousands of interleaved lines from concurrent requests.

Interview question

An Express app attaches a correlation id to a child logger for each incoming request. What problem does this specifically solve?

  • a.It reduces the total number of log lines written by batching them into a single entry per request
  • b.It automatically encrypts sensitive fields like passwords before they are written to the log
  • c.It replaces the need for log levels like debug, info, and error entirely
  • d.It lets every log line emitted while handling one request be traced back together, even across many concurrent requests in the same processCorrect
Why?

A per-request correlation id is exactly what lets you reconstruct one request's full path through interleaved concurrent logs by filtering on that id. It does not replace log levels, does not encrypt anything, and does not batch or reduce log volume, it is purely a correlation mechanism.

Just read this? Test yourself on what you have been reading.

Read the original → leadwithskills.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles