Winston: using default logger with timestamp logs twice

Created on 27 Jan 2018  路  1Comment  路  Source: winstonjs/winston

Hi,
I am using winston v2.4.0 in node v9.4.0. Using the default logger the log prints twice, once without timestamp and another same line with timestamp.
Code:

const winston = require('winston');
winston.add(winston.transports.Console, {
  name: 'mylog',
  timestamp: true
});

winston.info("I am printed twice");

output:

info: I am printed twice
2018-01-27T18:37:17.634Z - info: I am printed twice

Expected output was
2018-01-27T18:37:17.634Z - info: I am printed twice

Why is it printing twice?

Most helpful comment

@adityamertia Because the default winston logger already has a console transport declared on it. So by calling winston.add, you are actually configuring a _second_ transport - hence the duplicate message.

You should _remove_ the original, pre-configured console transport before adding your own. Try:

const winston = require('winston');
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {
  name: 'mylog',
  timestamp: true
});

winston.info("I am printed twice");

Note that this is no longer true on latest 3.x, where the default logger does not declare any transport out of the box.

>All comments

@adityamertia Because the default winston logger already has a console transport declared on it. So by calling winston.add, you are actually configuring a _second_ transport - hence the duplicate message.

You should _remove_ the original, pre-configured console transport before adding your own. Try:

const winston = require('winston');
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {
  name: 'mylog',
  timestamp: true
});

winston.info("I am printed twice");

Note that this is no longer true on latest 3.x, where the default logger does not declare any transport out of the box.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  4Comments

alditis picture alditis  路  3Comments

Nepoxx picture Nepoxx  路  4Comments

kjin picture kjin  路  3Comments

KingRial picture KingRial  路  3Comments