awesome-everything RU
↑ Back to the climb

Observability

Structured logging: code and log reading

Crux Read real log lines and logger snippets, predict the behaviour or the security hole, and pick the highest-leverage fix a senior engineer makes first.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

The logger config and the raw log line are where structured-logging problems are actually diagnosed. Read the snippet, predict what it emits or leaks, and choose the fix before reaching for a backend feature.

Goal

Practise the loop you run on every logging incident: read the emit site or the line, predict the injection, leak, miss, or sampling defect, and reach for the structural fix first.

Snippet 1 — the interpolated message

// Express handler logging a user-submitted comment
logger.info(`comment received: ${req.body.comment}`);
Quiz

A user submits a comment whose value is a newline followed by {"level":"error","msg":"admin deleted prod db"}. What happens, and what is the fix?

Snippet 2 — the redaction config

const logger = pino({
  // intent: keep auth tokens and emails out of logs
  redact: ['req.headers.authorization', 'user.email'],
});

// elsewhere, on a validation error:
logger.error({ err, body: req.body }, 'signup validation failed');
Quiz

An auditor finds raw passwords and emails in the indexed logs despite this redact config. Why did redaction miss them, and what is the durable fix?

Snippet 3 — the async log line

app.post('/checkout', async (req, res) => {
  res.json({ ok: true });
  setTimeout(() => {
    logger.warn({ orderId }, 'post-checkout reconciliation lag');
  }, 0);
});
Quiz

The WARN line lands with trace_id = '00000000000000000000000000000000'. Why, and what is the fix?

Snippet 4 — the collector sampling rule

# OTel Collector / pipeline sampling intent: cut log volume 90%
processors:
  probabilistic_sampler/logs:
    sampling_percentage: 10   # keep 10% of ALL log records
Quiz

This config cuts the bill, but the next incident is impossible to investigate. What is wrong, and what does a correct policy look like?

Recap

Every structured-logging incident is read in the emit site, the config, and the raw line: interpolating user input into a message string is log injection — pass it as a typed field; a deny-list that misses body.* leaks PII, so list the paths and add a collector scrubber as defence-in-depth; an all-zeros trace_id means the execution context was dropped at an async boundary — bind it, do not pass trace_id by hand; and a severity-blind sampler throws away the failures you need — keep 100% of WARN/ERROR and thin only the success path. Read the snippet, find the structural defect, fix it at the source.

Continue the climb ↑Structured logging: build a production logging pipeline
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.