Search code examples
javascriptnode.jsexpresswinston

How to create a transport from the Console one?


At the moment I have this code:

const logger = new winston.Logger();

logger.add(winston.transports.Console, {
  level: environment === 'development' ? 'silly' : 'info',
  colorize: true,
  prettyPrint: true
});

But I want to be able to write

const logger = new winston.Logger({transports: [customConsoleTransport]});

How can I create customConsoleTransport?


Solution

  • You can create a new instance of the winston.transports.Console as it's just a constructor function. You'll end up with something like this:

    const customConsoleTransport = new winston.transports.Console({
      level: environment === 'development' ? 'silly' : 'info',
      colorize: true,
      prettyPrint: true
    });
    
    const logger = new winston.Logger({transports: [customConsoleTransport]});