Correct logging to file using Node.js's Winston module

1.4k views Asked by At

I'm using Winston to do logging both to the console and to a file. If I specify a formatter AND the logged message is the same as the previous one, the file log is only written to once. In other situations (no formatter specified or writing to the console) the logging works as expected.

Here's simplified code:

var winston = require('winston');

function formatter(args) {
    return "some formatting: " + args.message;
}

var weirdLogger = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)({
            json: false,
            formatter: formatter
        }),
        new (winston.transports.File)({
            filename: "weirdLogger.csv",
            json: false,
            formatter: formatter
        })
    ]
});

var workingLogger = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)({
            json: false
        }),
        new (winston.transports.File)({
            filename: "workingLogger.csv",
            json: false
        })
    ]
});

var weirdLogTest = function () {
    weirdLogger.info("test1");
    workingLogger.info("test2");
};

var expectedBehaviorLogTest = function () {
    weirdLogger.info("test1: " + new Date().getTime());
    workingLogger.info("test2: " + new Date().getTime());
};

setInterval(weirdLogTest, 5000);
//setInterval(expectedBehaviorLogTest, 5000);

As written, this code shows test1 and test2 being logged to the console every five seconds and workingLogger.csv being updated, but weirdLogger.csv is not. Commenting out setInterval(weirdLogTest, 5000); and commenting in the expectedBehavior test shows that logging happens as expected when the date is appended to each log.

I've been unable to find anything in the docs about ignoring duplicate messages or other things that would explain this. Any ideas about what I'm doing incorrectly? It could be a bug, but I don't want to rule out user error.

1

There are 1 answers

0
Michiyo On BEST ANSWER

This was confirmed to be a bug that was caused not by the formatter but because my formatted log output was so short. There is a fix that will be merged.