principlejavascriptModerate
Log levels best practices: when to use debug, info, warn, error, fatal
Viewed 0 times
log leveldebug info warn error fataldynamic log levelproduction logginglog volumePII in logsisLevelEnabled
Problem
Everything is logged at INFO level, making it impossible to filter noise from signal in production. Or DEBUG logs are enabled in production, causing performance degradation and filling disk with gigabytes of useless output.
Solution
Use log levels consistently based on who needs to act and when:
Enable dynamic log level changes at runtime to enable DEBUG in production on demand without restarting.
- TRACE — ultra-verbose, individual function calls, never in production
- DEBUG — diagnostic info useful during development, disabled in production by default, enabled on-demand via dynamic log levels
- INFO — normal operational events (server started, request received, job completed)
- WARN — something unexpected happened but the system recovered (retried after failure, fallback used, deprecated API called)
- ERROR — an operation failed and requires attention but the service continues running (request failed, DB query error)
- FATAL — unrecoverable error, service is about to crash (startup failure, invalid config)
// Good log level usage
logger.info({ port: 3000 }, 'Server listening'); // startup event
logger.warn({ attempt: 3 }, 'Retrying payment API'); // recovered from failure
logger.error({ err, orderId }, 'Failed to process order'); // operational error
logger.fatal({ err }, 'Database connection refused on startup'); // unrecoverableEnable dynamic log level changes at runtime to enable DEBUG in production on demand without restarting.
Why
Correct log levels allow operators to set LOG_LEVEL=warn in production to see only actionable signal, then raise to LOG_LEVEL=debug for specific services during an incident without noise from all other services.
Gotchas
- LOG_LEVEL=debug in production is dangerous — query parameters, headers, and response bodies often contain PII
- Warn should imply no action required now — if immediate action is needed, use error
- Logging inside hot loops at INFO or higher is a performance killer — guard with logger.isLevelEnabled('debug')
- Some frameworks log every request at INFO — this creates enormous log volume; prefer logging only errors at the framework level
Context
Establishing logging standards for a team or auditing log quality in an existing codebase
Revisions (0)
No revisions yet.