Custom Transports

Winston supports creating custom transports to log messages in unique ways. Here’s a basic example of a custom transport:

const { TransportStreamOptions } = require('winston-transport');

class CustomTransport extends TransportStreamOptions {
  constructor(options) {
    super(options);
    this.level = options.level || 'info';
  }

  log(info, callback) {
    setImmediate(() => this.emit('logged', info));
    console.log(`${info.level}: ${info.message}`);
    if (callback) callback();
  }
}

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.simple(),
  transports: [
    new CustomTransport({ level: 'info' })
  ]
});

logger.info('This is a custom transport log message');

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *