1

I am writing a module for logging in Nodejs using Winston. I am using a custom format and initialize my logger like this:

const logger = createLogger({
  format: combine(
    format.timestamp(),
    format.printf(msg => `${JSON.stringify({timestamp: msg.timestamp, 
                                            shortmessage: msg.message, 
                                            level: msg.level, 
                                            source: config.programName, 
                                            file: __filename,
                                            line: '' })}`)   // how to get this?
  ),
  transports: [new (transports.Console)({
    level: config.logLevel, // logs up to specified level
  })]
});


module.exports = {
  error: function (message) {
    logger.error(message);
  },
  info: function (message) {
    logger.info(message);
  },
  debug: function (message) {
    logger.debug(message);
  }
};

As mentioned in the comment, I also need to include line number in my log. I did some research and found some workarounds (1, 2, 3), but it seems they can't be used in my case, as I use a custom format, which should be specified during the logger creation time and line number can be retrieved later.

I was thinking to use Winston's label feature, but it seems labels can contain only static data.

How can this be solved? Any ideas.

nabroyan
  • 3,225
  • 6
  • 37
  • 56

0 Answers0