Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different formatter for different transports #1567

Closed
1 of 2 tasks
senseysensor opened this issue Jan 16, 2019 · 5 comments
Closed
1 of 2 tasks

Different formatter for different transports #1567

senseysensor opened this issue Jan 16, 2019 · 5 comments
Labels
Docs Applies to Winston's documentation Good First Issue Good issue for new contributors Important

Comments

@senseysensor
Copy link

senseysensor commented Jan 16, 2019

Please tell us about your environment:

  • winston version?
    • winston@2
    • 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 #1269

@senseysensor senseysensor changed the title Different formatter to different transports Different formatter for different transports Jan 17, 2019
@indexzero
Copy link
Member

@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()
      )
    })
  ]
});

@indexzero indexzero added Docs Applies to Winston's documentation Important Good First Issue Good issue for new contributors labels Jan 26, 2019
@cjbarth
Copy link

cjbarth commented Oct 2, 2019

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

kwiatkk1 added a commit to kwiatkk1/winston that referenced this issue Oct 24, 2019
DABH pushed a commit that referenced this issue Oct 27, 2019
* #1567: document common transport options

* Update README.md
@DABH DABH closed this as completed Oct 27, 2019
Mizumaki pushed a commit to Mizumaki/winston that referenced this issue Jun 11, 2020
* winstonjs#1567: document common transport options

* Update README.md
@egalvan10
Copy link

egalvan10 commented Apr 19, 2022

Is formatting not supported for transports.Http()? I am able to see formatting in the console transport, but not in the server receiving from Http one. Specifically trying to do some string manipulation before sending.

@struginskij
Copy link

@egalvan10 Same here

@dgodevais
Copy link

I'm also having issues with applying formatting to the Http transport while the other ones are working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Applies to Winston's documentation Good First Issue Good issue for new contributors Important
Projects
None yet
Development

No branches or pull requests

7 participants