Winston: The Ultimate Logger

Fateh Ali Aamir
3 min readFeb 14, 2024

A logger for just about everything. Winston is a popular logging library for Node.js applications that provides flexible logging options. We’ll get into the awesome stuff further down.

How to Setup

npm install winston

Once you’ve got it installed, it is super easy to set it up.

import winston from 'winston';

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

export default logger;

Now let’s analyse what’s happening here by studying each feature. Apart from that, this is the complete set of parameters that are included in a logger configuration:
level: Log only if info.level is less than or equal to this level
levels: Levels (and colors) representing log priorities
format: Formatting for info messages
transports[]: Set of logging targets for info messages
exitOnError: If false, handled exceptions will not cause process.exit
silent: If true, all logs are suppressed

Feature: Level

There are many levels that are available in Winston and you can use them as per your requirement. These levels can help you categorize your logs and it can be very useful when you’re actually working with a very dense and complicated code.

{
error: 0,
warn: 1,
info: 2,
http: 3,
verbose: 4,
debug: 5,
silly: 6
}

This is how you can call different levels of logs when logging in your code. It is extremely simple.

logger.log('error', "127.0.0.1 - there's no place like home");
logger.info("127.0.0.1 - there's no place like home");
logger.warn("127.0.0.1 - there's no place like home");
logger.error("127.0.0.1 - there's no place like home");

Feature: Format

The format can be very crucial when logging and Winston offers us a few interesting options for it. Here are some of the parameters it accepts:
splat( ): String interpolation splat for %d %s -style messages.
timestamp( ): timestamp the message was received.
label( ): Custom label associated with each message.
ms( ): Number of milliseconds since the previous log message.

You can use many types of formats for your message as well. Namely json, logstash, printf, prettyPrint, simple. And any number of formats may be combined into a single format using format.combine( ). And last but not the least you can actually color up your logs by using format.colorize( ).

Feature: Transports

There are two basic types of transports. One that goes to the console and the other one that goes to a file.

const transports = {
console: new winston.transports.Console({ level: 'warn' }),
file: new winston.transports.File({ filename: 'combined.log', level: 'error' })
};

As seen above, can specify a level for your logs or you just leave it blank and make it a common transport. For the file transport you can even define you file name and do much more.

Feature: Daily Rotate

npm install winston-daily-rotate-file

This is a separate package that you can install that will let you perform rotations for your log files. The rotation system is also extremely flexible and you can set it up as you wish. It also falls into the transports feature and can be very useful when you need to segment your log files.

import winston from 'winston';
import DailyRotateFile from 'winston-daily-rotate-file';

const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.simple()
),
transports: [
new winston.transports.Console(),
new DailyRotateFile({
filename: 'logs/%DATE%-error.log',
datePattern: 'YYYY-MM-DD',
level: 'error'
}),
new DailyRotateFile({
filename: 'logs/%DATE%-combined.log',
datePattern: 'YYYY-MM-DD'
})
]
});

export default logger;

Now your file can file rotate according to the datePattern that you set up. It can even for minutes or hours. Once a rotation is complete, the previous set of files can be deleted as well, depending on the configurations.

Why Use A Logger

  • It can help you when debugging and troubleshooting.
  • It can improve you monitoring and performance.
  • It can make your auditing and compliance more transparent.

So when you’re creating a full-fledged application which is very expansive, it’s better to not use console.log( ) and actually have a mechanism in place that can let you persist your data your data as well. It might not seem like a neccessary operation but it can be very fruitful in the long run. And there is no better option than Winston.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Fateh Ali Aamir
Fateh Ali Aamir

Written by Fateh Ali Aamir

23. A programmer by profession. A writer by passion.

No responses yet

Write a response