Node.js Developer interview question
How do you design logging for Node.js?
Emit structured logs with timestamp, severity, service and request or trace ID; log decisions and failures once at the owning boundary, redact secrets and personal data, and avoid blocking or high-cardinality noise.
Direct answer
What a strong answer covers
How it works
Logs are JSON lines with a timestamp, level, service name, version, and the request or trace ID, written by a library such as pino that serialises asynchronously and cheaply. A request gets one log line at the boundary with method, route, status, and duration; business events and failures are logged once where the decision is made, not at every layer they pass through. Personal data and secrets are redacted by configuration before serialisation, high-cardinality values stay out of log messages, and log level is adjustable at runtime so debug output can be enabled for one incident without a deploy.
Example
A pino logger with a redact list for authorization headers and password fields is attached to each request via a child logger carrying requestId; the handler logs order.created once with orderId and amount, and an error middleware logs the failure once with the stack. During an incident the level is raised to debug through an admin endpoint for five minutes, then lowered.
Common mistakes
- Logging the same error at every layer and multiplying noise.
- Using console.log in production, which is synchronous on some streams and blocks the loop.
- Writing user emails, tokens, or request bodies into logs that are shipped to a third-party platform.
A useful answer structure
Answer from your own work.
Do not memorise a script: use only examples you can explain honestly in follow-up questions.
Open the desktop workspace and rehearse a clear answer in your own words.