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.
Never log authorization headers, passwords, cookies, private keys, refresh tokens, or other sensitive credentials.
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.
Use structured logs for production systems.
Include correlation identifiers when tracing requests across services.
Redact sensitive values before logging.
Which value should normally never be logged in plaintext?