What is Winston and why is it needed?
Winston is a popular logging library for TypeScript and JavaScript. It provides a flexible logging system with support for multiple transports (console output, file, database, HTTP), logging levels, and message formatting. In large projects, logging is critical for debugging, monitoring, and auditing user or system actions. Winston allows you to structure logs, filter them by importance, and save them in a format convenient for analysis.
How to install Winston in TypeScript
To install, run the command in the terminal:
npm install winstonIt is also recommended to install types for TypeScript:
npm install --save-dev @types/winstonMain features of Winston
- Logging levels: error, warn, info, verbose, debug, silly (configurable).
- Transports: Console, File, HTTP, MongoDB, Syslog, and others.
- Formatting: JSON, plain text, timestamps, colors.
- Asynchronicity: does not block the main thread.
- Custom profiles: creating your own levels and transports.
Code example in TypeScript
import winston from 'winston';
const logger = winston.createLogger({ level: 'info', format: winston.format.json(), transports: [ new winston.transports.Console({ format: winston.format.simple(), }), new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: 'combined.log' }), ],});
logger.info('Application started');logger.error('Database connection error');This code creates a logger that writes all messages of level info and above to the console, errors to error.log, and all logs to combined.log.
When to use Winston
Winston is suitable for any server-side Node.js applications (Express, NestJS, Koa), microservices, CLI utilities, and complex scripts. It is especially useful in production environments where centralized log storage, file rotation, and integration with monitoring systems (ELK, Splunk) are required. For simple projects, you can use the built-in console.log, but Winston provides control and scalability.