winstonjs/logform

Winston logform library does not have a unified way to return formatted message

losalex opened this issue · 0 comments

The code sample const { level, message, ...meta } = info; described here does not always returns message consistently and may vary depending if format object was provided to winston.createLogger() or not. It appears that info parameter could also have an info[Symbol.for('message')] in addition to info.message field and those values are not the same:

  1. In case when no formatting is provided to winston.createLogger() constructor, the correct message indeed returned by info.message.
  2. When formatting is provided to winston.createLogger() constructor, the correct message will be in info[MESSAGE] field, while info.message contains non-formatted version of the message.

Given a fact that we don't have a way to see if formatting was provided for winston.createLogger() constructor or not, we cannot provide a deterministic approach to fetch correct message (e.g. we cannot count on info[MESSAGE] nor on info.message fields to determine where is a "right" message is).

The code which can be used to test the issue is:

const logger = winston.createLogger({
    level: LOG_LEVEL,
    format: winston.format.combine(
        winston.format.timestamp({format: 'YYYY-MM-DD HH:mm:ss,SSS'}),
        winston.format.printf(info => `${info.timestamp} |${info.level.toUpperCase()}| my-service -> ${info.message}`)),
    transports: [
        // Add custom transport here to intercept the info for debugging purposes
        //new SomeTransportLogging(), 
        new winston.transports.Console()
    ]
});