Categories
JavaScript Answers

How to add timestamp to logs using Node.js library Winston?

Spread the love

To add timestamp to logs using Node.js library Winston, we set the transports property to an array with the Winston console object with the timestamp option set to true.

For instance, we write

const winston = require("winston");
const logger = new winston.Logger({
  transports: [new winston.transports.Console({ timestamp: true })],
});

to create the Logger with the transports property set to an array with an array of loggers.

We create a logger with the Console constructor by calling it with an object with the timestamp property set to true to add a timestamp to each log entry.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *