Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding different formatters to Winston transports

I need a different types of formatters for each transport.

Example:

logger = new (winston.Logger)({
    transports: [
        new (winston.transports.LogstashUDP)({
            // some config here. Do noting on formatting
        }),
        new (winston.transports.Mail)({
            // do formatting one way
        }),
        new (winston.transports.File)({
            // write to file as json (maybe format it here)
        }),
        new (winston.transports.Console)({
            // do another formatting
        })
    ]
});

As i can see from winston transports docs only Console supports custom formatter.

I'm using winston-mailer module for mail and winston-logstash-upd

Is there any way to solve this with Winston? Or maybe how to create wrapper around one of these modules to support formatting?

like image 680
coockoo Avatar asked Oct 06 '15 11:10

coockoo


Video Answer


1 Answers

Here's the solution posted to GitHub by dandv

const logger = winston.createLogger({
  transports: [
    new winston.transports.File({
      filename: 'error.log', level: 'error',
      format: winston.format.simple(),
    }),
    new winston.transports.File({
      filename: 'combined.log', level: 'debug',
      format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),
    }),
  ],
});

logger.error('prefixed by the timestamp only in `combined.log`');
like image 83
CodyBugstein Avatar answered Sep 19 '22 13:09

CodyBugstein