Search code examples
javascriptnode.jsloggingwinston

Winston logger appends "undefined" to every log entry


In a node.js / express.js - based application the winston logging framework (version 2.4.0) is used for logging. That works fine, except that undefined is appended to every log output. The type of output (string, object, ..) does not change that behaviour. Winston is used from several .js files like this:

const logger = require('winston')
..
logger.info(`Handled request in ${Date.now() - start}ms.`);

The output for the above log statement is:

info: Handled request in 667ms.undefined

When logger.{info|debug|..} is replaced with a statement that logs to console the output is as expected; just the statement(s) to be logged with the 'undefined' concatenated at the end.

What might be the cause?


Solution

  • I'm not sure what is the reason for this and I don't really have time to dig into the problem but for whatever reason the example code provided by winston creators didn't work properly for me and appended this 'undefined' after every line. Just as in your case.

    I did some quick search for 'eol' in the winston module and in the console.js in Console function I printed all the options, and eol parameter. It turned out that somehow as default no eol is specified and thus this 'undefined' appendix. In fact any options that I used in winston.configure() didn't make it to the Console function. But if we create new Console explicitly and pass options to it (including eol) it works pretty well.

    tl;dr; I advise you to try this code to initialize your winston logging and modify it to suit your needs:

    const winston = require('winston');
    winston.configure({
      transports: [
        new winston.transports.Console({
          level: 'silly',
          eol: '\n',
          timestamp: true,
          colorize: true,
        })
      ]
    });
    winston.silly('testing')
    

    It is more of a workaround then a real solution but hey, it works, right?

    EDIT: If you are on windows you might want to try '\r\n' eol instead.