Production Logging

Production applications should use structured logs containing fields such as timestamp, severity, service, request ID, user or correlation identifiers where appropriate, and useful error metadata.

Avoid logging passwords, access tokens, session cookies, API keys, and other sensitive information. JSON logs are convenient for centralized log systems.

Redact Secrets

Never log authorization headers, passwords, cookies, private keys, refresh tokens, or other sensitive credentials.

Structured Logger
function log(level, message, metadata = {}) {
  console.log(JSON.stringify({
    timestamp: new Date().toISOString(),
    level,
    message,
    ...metadata
  }));
}

log('info', 'Request completed', {
  requestId: 'req-123',
  statusCode: 200
});
💡 Structured logs make application events easier to search, filter, aggregate, and analyze.
Key Takeaways
  • Use structured logs for production systems.
  • Include correlation identifiers when tracing requests across services.
  • Redact sensitive values before logging.
Knowledge Check

Which value should normally never be logged in plaintext?