coockoo
coockoo

Reputation: 2382

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?

Upvotes: 11

Views: 2528

Answers (1)

CodyBugstein
CodyBugstein

Reputation: 23322

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`');

Upvotes: 5

Related Questions