Winston: Different formatter for different transports

Created on 16 Jan 2019  路  2Comments  路  Source: winstonjs/winston

Please tell us about your environment:

  • _winston version?_

    • [ ] winston@2

    • [x] winston@3

  • _node -v outputs:_ v10.9.0
  • _Operating System?_ os x 10.14.2
  • _Language?_ ES6/7

What is the problem?

We need to have possibility to configure different formatter for each transport. As is described here on SO: https://stackoverflow.com/q/32968838/1002036

It should be obvious that we don't want to have the same formatter for console (i.e., string with padding and colors) and for, for example, mongoDB (json object). So for the moment the only solution I can see is to make some wrappers that would call different loggers, each with single transport and formatter.

Probably is a bit related to https://github.com/winstonjs/winston/issues/1269

docs good first issue important

Most helpful comment

@senseysensor that is absolutely a supported feature of winston@3. This is used in several examples, but is not _(surprisingly)_ not documented in our README.md. The canonical example would be in ./examples/quick-start.js. But to summarize, each Transport accepts a format. e.g.:

const { createLogger, format, transports } = require('../');

const logger = createLogger({
  level: 'info',
  //
  // All Transports get:
  // - Timestamp
  // - Error handling (i.e. logger.log(new Error('loloksure')))
  // - String interpolation
  //
  format: format.combine(
    format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss'
    }),
    format.errors({ stack: true }),
    format.splat()
  ),
  defaultMeta: { service: 'your-service-name' },
  transports: [
    //
    // File transports serialize to JSON
    // 
    new transports.File({ 
      format: format.json(),
      filename: 'quick-start-error.log', 
      level: 'error'
    }),
    new transports.File({ 
      format: format.json()
      filename: 'quick-start-combined.log' 
    }),
    //
    // Console transport is colored and simply
    // formatted.
    //
    new transports.Console({
      format: format.combine(
        format.colorize(),
        format.simple()
      )
    })
  ]
});

All 2 comments

@senseysensor that is absolutely a supported feature of winston@3. This is used in several examples, but is not _(surprisingly)_ not documented in our README.md. The canonical example would be in ./examples/quick-start.js. But to summarize, each Transport accepts a format. e.g.:

const { createLogger, format, transports } = require('../');

const logger = createLogger({
  level: 'info',
  //
  // All Transports get:
  // - Timestamp
  // - Error handling (i.e. logger.log(new Error('loloksure')))
  // - String interpolation
  //
  format: format.combine(
    format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss'
    }),
    format.errors({ stack: true }),
    format.splat()
  ),
  defaultMeta: { service: 'your-service-name' },
  transports: [
    //
    // File transports serialize to JSON
    // 
    new transports.File({ 
      format: format.json(),
      filename: 'quick-start-error.log', 
      level: 'error'
    }),
    new transports.File({ 
      format: format.json()
      filename: 'quick-start-combined.log' 
    }),
    //
    // Console transport is colored and simply
    // formatted.
    //
    new transports.Console({
      format: format.combine(
        format.colorize(),
        format.simple()
      )
    })
  ]
});

Beware that this feature is not entirely working correctly for Error objects. See #1338.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amiram picture amiram  路  4Comments

mohanen picture mohanen  路  4Comments

sinai-doron picture sinai-doron  路  3Comments

kjin picture kjin  路  3Comments

Buzut picture Buzut  路  3Comments