Lesson 2 of 13 · 18m
Structured Logging: Fields, Not Sentences
One JSON line per event with real fields — what to emit, what never to write, and how retention tiers keep the bill honest.
Your app writes two thousand log lines a minute and none of them answer anything: GET /healthz 200 a hundred times a minute from the load balancer, Starting request with no request ID, a stack trace split across twelve fmt.Println calls. The volume is a bill and the content is noise. Structured logging is not a format preference — it is the difference between grep and a query.
The log line that pays
One line, one event, fields:
{"ts":"2026-11-16T14:22:01Z","level":"error","msg":"payment charge failed","trace_id":"4bf92f3577b34da6","order_id":"ord_913","provider":"stripe","error":"card_declined","latency_ms":231}Every field on that line is filterable in Loki, Elasticsearch, or Datadog without regex. level is where most teams lie first: if everything logs at INFO, the level field is decorative. The working convention: DEBUG you turn on for a problem, INFO is normal operation worth reading, WARN is "still working, worth knowing," ERROR is "a user-visible thing failed." FATAL exits the process — there is no FATAL in library code, no matter how dramatic the exception felt. And an ERROR at startup that the app survives trains everyone that ERROR means nothing.
The request-log recipe that works
Skip the Starting request / Finished request pair — two lines per request doubles volume and still needs joining. Log once at the end of each request: route (the template, not the URL), status, latency_ms, trace_id. Hot paths get sampled below that: keep every non-2xx and 1% of successes, or drive it through a sampler handler — slog accepts custom Handlers, pino takes transports and level per child logger.
app.use((req, res, next) => {
const t0 = process.hrtime.bigint();
res.on("finish", () => {
const ms = Number(process.hrtime.bigint() - t0) / 1e6;
if (res.statusCode < 500 && Math.random() > 0.01) return;
logger.info({ route: req.route?.path ?? "unmatched", status: res.statusCode,
latency_ms: Math.round(ms), trace_id: req.traceId }, "request");
});
next();
});That is the whole pattern: fields, one line, errors kept, baseline sampled. Nothing about it is clever, which is why it works.
What never goes in a log
Authorization headers, session tokens, passwords, card numbers, and honestly most PII. Not because it is rude — because a leaked log bucket is a credential dump, and "the logs got exposed" is a breach report you have now written. Provider keys have been found in CI logs and in application logs shipped to third-party SaaS; the pipeline has a longer memory than your incident review.
Scrub at the logger, not by convention: a middleware or log-hook that drops authorization, cookie, and your provider's field names before a line is emitted beats hoping every developer remembers. /healthz hits belong in a metric counter, not a log line — the classic failure is per-request logging on a 10k RPS path: ten thousand billed lines a second teaching you nothing.
Retention is a decision, not a default
Hot logs — indexed, fast search — are where debugging lives; 7–14 days is the working range. Cold logs go to object storage for whatever compliance demands, typically 90 days. Forever is not a tier. It is a bill and a liability: every line you keep is a line that can be subpoenaed, breached, or invoiced.
Concrete practice
- One JSON line per event:
slogfor Go,pinofor Node,structlogfor Python, Serilog for .NET. trace_idon every line — the cheapest correlation in observability.- An allowlist middleware for headers and fields, not a developer's memory.
- Log the event, not the sentence:
"msg":"charge failed","error":"insufficient_funds"beats"Charge for order ord_913 failed: insufficient funds"— the first is a filter, the second is a regex. - Audit the top-N log lines by volume monthly. The top offender is always noise.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Your Container Is Running SerialGC and Nobody Told You
You tuned G1 flags for a week. Your pod has one CPU, which means the JVM quietly picked a single-threaded collector at startup and ignored every flag you set. Here is the OpenJDK source that made that decision.
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.
Your npm Install Just Ran Someone Else's Code: Why Dependency Security Has to Start Before CI
A pull request checkout, a poisoned GitHub Actions cache, and an OIDC token pulled straight out of runner memory. That's how 84 malicious versions of TanStack's packages hit npm in six minutes.