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

Formatting example #1615

Open
tvld opened this issue Mar 8, 2019 · 5 comments
Open

Formatting example #1615

tvld opened this issue Mar 8, 2019 · 5 comments

Comments

@tvld
Copy link

tvld commented Mar 8, 2019

It took me long time to figure out some simple formatting. The examples I found with a formatter function, simply did not work for me. Finally I got this working:

  logger.add(new winston.transports.File({
    level: 'info', // Log this level and all higher priorities
    filename: 'myfile.log',
    format: winston.format.combine(
      winston.format(options => {
        return {
          'level': options.level,
          'message': options.message,
          'my_own_nr': 12345,
          'nice_text': 'hello there'
        }
      })(),
      winston.format.timestamp(),
      winston.format.json()
    )
  }))

Maybe good to add to documentation?

@xiulunlin
Copy link

+1
Don't know how to configure

@DABH
Copy link
Contributor

DABH commented Mar 24, 2019

Thanks! Could you clarify what this does that none of the examples in the readme or examples folder https://github.com/winstonjs/winston/tree/master/examples do? Just so we know how to call this snippet if we add it to the examples...

@tvld
Copy link
Author

tvld commented Mar 25, 2019

Good point. The search and retrieve for examples is often the hardest part ...
I suppose we could call it some kind of "simple direct json formatting" ?

@fourpastmidnight
Copy link

I also had trouble understanding how the formatters work. I set up this codesandbox.

What I've learned is that a format specified when creating the logger sets up a "global" or "base" format. In my case, I tend to use format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSSZZ' }). Then, in a transport, you can specify another format. That format can be composed via format.combine(formatters: Format[]). And at runtime, the logger format that was specified is then "combined" with the transport format(s) to obtain the resulting log entry format.

Note that some "formats" simply "mutate" properties of the log entry, such as the level, or the message, while others actually specify an output format, e.g. format.json(). You must have, at most, one "output format". (I actually don't know what happens if you have more than one--but I don't think you'll get the expected results, given what I've learned about Winston.) Be aware that format.combine creates a "pipeline" of formatters (or, as I like to call them, mutators). They will be applied in the order in which they appear in the array. So, if you specify an "output format" first, then later "mutating formatters" may not work.

Also, be aware if you are using multiple transports. I've come across what appears to be a nasty bug in Winston, where any transports after the first transport DO NOT get all the metadata that may be present on the info object: #1933.

@fourpastmidnight
Copy link

There is a comment on #1430 which resolves the issue with info "losing data" between transports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants