Winston for JavaScript (Node.js): Complete Guide with Examples

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?

\n\n

Winston is a powerful and flexible logging library for the Node.js environment. It was created by Charlie Robbins and is one of the most popular logging libraries in the JavaScript ecosystem. Winston allows developers to record messages with various severity levels (info, warn, error, debug, etc.), configure output formats (JSON, plain text, with timestamps), and route logs to different destinations (console, files, databases, external services).

\n\n

Why is Winston needed? In any serious application, logging is not a luxury but a necessity. It helps:

\n
    \n
  • Debug the application: Logs allow you to understand what was happening in the system at the time of an error.
  • \n
  • Monitor performance: You can record request execution times and identify bottlenecks.
  • \n
  • Analyze user behavior: Logs help understand how users interact with the application.
  • \n
  • Maintain history: All events can be stored in files or databases for subsequent analysis.
  • \n
  • Ensure security: Logs can record suspicious actions and intrusion attempts.
  • \n
\n\n

Winston solves these tasks elegantly and efficiently by providing a unified interface for working with logs.

\n\n

Installing Winston

\n\n

Installing Winston is done via the npm package manager. Open a terminal in your project's root folder and run the command:

\n\n
npm install winston
\n\n

If you use yarn:

\n\n
yarn add winston
\n\n

After installation, you can import Winston into your project:

\n\n
const winston = require('winston');
\n\n

For projects using ES modules (type: "module" in package.json):

\n\n
import winston from 'winston';
\n\n

Quick Start with Code Example

\n\n

Let's create a simple logger that outputs messages to the console and to a file:

\n\n
// logger.js\nconst winston = require('winston');\n\n// Create a logger with two transports: console and file\nconst logger = winston.createLogger({\n    level: 'info', // Minimum logging level\n    format: winston.format.json(), // Output format — JSON\n    transports: [\n        new winston.transports.Console(), // Output to console\n        new winston.transports.File({ filename: 'combined.log' }) // Write to file\n    ]\n});\n\n// Use the logger\nlogger.info('Application started');\nlogger.warn('Warning: low memory');\nlogger.error('Database connection error');\n\nconsole.log('Logs written. Check the combined.log file');
\n\n

After running this code, you will see logs in the console and also find the file combined.log in the project's root folder.

\n\n

Main Methods and Functions of Winston

\n\n

Winston provides a rich API. Let's look at 10 key methods and functions with examples.

\n\n

1. winston.createLogger(options)

\n

What it does: Creates a new logger instance with the specified configuration. This is the primary way to create a logger.

\n

Parameters:

\n
    \n
  • options.level (string): The minimum logging level (e.g., 'info', 'debug', 'error').
  • \n
  • options.format (Format): The output format for logs (e.g., winston.format.json(), winston.format.simple()).
  • \n
  • options.transports (Transport[]): An array of transports that determine where to send logs.
  • \n
  • options.defaultMeta (Object): Metadata that will be added to every message.
  • \n
  • options.exitOnError (boolean): If true, the process will exit on a logging error (default is true).
  • \n
\n

Example:

\n
const lo                    

Recommendations