Why does Winston output `undefined` with custom format?

Viewed 764
let logger = winston.createLogger({
     format: {
         transform(info, opts) {
             info.message = "!" + info.message + "!";
             return info;
         }
    },
    transports: [
         new winston.transports.Console()
     ]
});
logger.info("Something");

This code outputs "undefined" in my console. Why? If I use the default formats, it works perfectly. But if I use my own or even custom formats from official winston examples, it does not work.

2 Answers

Maybe it's different for each version

const logger = winston.createLogger({
    transports: [
        new winston.transports.Console({
            format: winston.format.printf(info => `!${info.message}!`)
        })
    ]
});
  1. as KiDoo Song has mentioned, you need to put format into the options of the transport.

  2. in order to build the plugin by yourself, you should use symbol type as the key instead of string:

    info[Symbol.for('message')] = `!${info.message}!`;
    
Related