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?
@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.
Most helpful comment
@adityamertia Because the default
winstonlogger already has a console transport declared on it. So by callingwinston.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: