Node.js › Logging & Monitoring

What is log level (debug, info, warn, error), and how do you configure a logger like Pino or Winston to filter output by level per environment?

Intermediate

Answer

Log levels categorize the severity/importance of a log entry, letting a logger be configured to only actually output entries at or above a certain threshold -- typically 'debug' level (very verbose, useful during active development) is enabled locally but suppressed in production, where only 'info' and above are emitted, reducing log volume and cost while still capturing everything operationally relevant; 'error' level logs are reserved for genuine failures needing attention.
const pino = require('pino')({
  level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
});

pino.debug('Detailed diagnostic info'); // only shown in development
pino.info('Server started');            // shown in both
pino.error({ err }, 'Payment processing failed'); // always shown, needs attention
Real-world example A production service configured with log level 'info' silently drops thousands of verbose debug-level log lines per minute that would otherwise be generated during normal operation, keeping log storage costs manageable while a developer can temporarily set the level to 'debug' via an environment variable when actively investigating a specific issue.

Common follow-ups: How would you dynamically change a running production service's log level without restarting it, for temporary deep debugging?;What's the risk of setting the production log level too high (like only 'error'), potentially missing useful warning signs before an actual failure?