foundation
Logging
Emit structured logs with request context, levels, redaction, correlation IDs, and production-friendly volume.
Production Node services emit structured logs (JSON lines) so aggregators can query by fields — not unstructured `console.log` strings.
logger.info({
reqId: req.id,
userId: req.user?.id,
method: req.method,
path: req.path,
durationMs: Date.now() - start,
status: res.statusCode,
}, 'request completed');
Levels: error/warn for actionable signals; info for request lifecycle; debug/trace gated by env. Redact passwords, tokens, PAN, and health payloads. Bind correlation/request ID at middleware and pass through async context (AsyncLocalStorage).
Volume control: sample debug logs; avoid logging full bodies; use metrics for high-cardinality counts.
On interviews: structured vs text; correlation across microservices; what never belongs in logs.
Common pitfalls: logging secrets on error paths; multiple logger formats; synchronous logging under load blocking the event loop (choose performant logger or async transport).
The trade-off is balancing simplicity, performance, safety, and operability — name which axis you optimized and what cost you accepted.
Checklist:
- JSON schema stable across services.
- Request ID in every log line for a trace.
- Redaction rules tested.
- Log level configurable per environment.