Winston: Allow configuring formats per-transport within the same logger

Created on 8 Jul 2018  ·  5Comments  ·  Source: winstonjs/winston

What's the feature?

I have a logger with a console transport, and another transport that goes to a cloud-based logging service. For the console, I want to prefix messages with a timestamp, and colorize them. For the cloud-based logging service, I don't want timestamps because the service provides its own, and I don't want colors either.

Is this a feature you're prepared to implement, with support from us?

PRs for this feature were already sent: #427 and #422

Is configuring formats per transport already possible, but I haven't seen that capability explained in the README?

Most helpful comment

Nevermind, this is indeed possible, but the documentation completely omits mentioning the feature.

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

All 5 comments

Nevermind, this is indeed possible, but the documentation completely omits mentioning the feature.

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

Definitely add to docs plz

@dandv The printf code in your comment does not work out of the box, there is a missing closing brace after the timestamp code.

So this line...

format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),

...needs a closing brace like so

format: winston.format.printf(info => `${new Date().toISOString()}, ${info.message}`),

Thanks for the code and knowledge for custom formatting! Much appreciated!

Nevermind, this indeed possible, but the documentation completely omits mentioning the feature.

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

@dandv what if I have another format outside of transports and inside the create logger method. Which format will be applied?

Nevermind, this indeed possible, but the documentation completely omits mentioning the feature.

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

@dandv what if I have another format outside of transports and inside the create logger method. Which format will be applied?

The one inside.

Was this page helpful?
0 / 5 - 0 ratings