Logging for TypeScript: Winston Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Winston and why is it needed?

Winston is a powerful and flexible logging library for TypeScript and JavaScript applications. It allows you to write logs to various places (console, files, databases) with different severity levels. Unlike simple console.log, Winston gives you full control over log format, rotation, and filtering.

Why use Winston? In large projects, standard console output becomes useless: logs get mixed up, lost, and cannot be analyzed. Winston solves these problems: you can configure error logging to a separate file, informational messages to the console, and debug data to a database. This is critical for debugging, monitoring, and auditing.

Installation

To install Winston in a TypeScript project, run the command:

npm install winston

It is also recommended to install the types:

npm install --save-dev @types/winston

If you use yarn:

yarn add winston

Main Features

  • Logging levels: error, warn, info, http, verbose, debug, silly. Each level has a priority, and you can configure which levels are output in different transports.
  • Transports: ways to output logs. Built-in: Console, File, Http, Stream. You can create your own.
  • Formats: customizing the appearance of logs (JSON, timestamp, colored output).
  • File rotation: via the additional module winston-daily-rotate-file, you can automatically create new log files by day or size.
  • Metadata: passing additional context (e.g., request ID, username).

TypeScript Code Example

Let's create a simple logger with console and file output:

import winston from 'winston';

const logger = winston.createLogger({ level: 'info', format: winston.format.combine( winston.format.timestamp(), winston.format.json() ), transports: [ new winston.transports.Console({ format: winston.format.combine( winston.format.colorize(), winston.format.simple() ) }), new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: 'combined.log' }) ]});

// Usagelogger.info('Server started on port 3000');logger.error('Database connection error', { database: 'main' });

In this example: informational messages are output to the console (colored) and to the combined.log file, while errors are additionally output to error.log. The log format is JSON with a timestamp.

When to use Winston?

  • In a production environment for error monitoring.
  • In a microservice architecture for collecting logs in a centralized storage.
  • When user action auditing is required.
  • In projects where log analysis is needed using ELK (Elasticsearch, Logstash, Kibana) or other systems.

Winston is the industry standard for logging in TypeScript. It is easy to configure, yet scales to complex requirements.

Recommendations