Winston Logger

3.19.0 · active · verified Sat Apr 18

Winston is a versatile logging library for JavaScript and Node.js, designed to support multiple transports for logs. It allows flexible configuration of log formatting and levels, decoupling the logging process from storage implementation. The current stable version is 3.19.0, with regular patch and minor releases addressing fixes and new features.

Common errors

Warnings

Install

Imports

Quickstart

Initializes a Winston logger with file transports for error and combined logs, adding a console transport for non-production environments, and demonstrates various log levels.

import winston from 'winston';

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  defaultMeta: { service: 'user-service' },
  transports: [
    // Log all errors to error.log
    new winston.transports.File({ filename: 'error.log', level: 'error' }),
    // Log all info and higher to combined.log
    new winston.transports.File({ filename: 'combined.log' })
  ]
});

// Add a console transport if not in production
if (process.env.NODE_ENV !== 'production') {
  logger.add(new winston.transports.Console({
    format: winston.format.simple()
  }));
}

// Example logging
logger.info('User logged in', { userId: 123 });
logger.warn('Deprecated feature used');
logger.error('Failed to connect to database', { error: 'Connection refused' });

view raw JSON →