rs / zerolog

Zero Allocation JSON Logger
MIT License
10.33k stars 564 forks source link

[QUESTION] Implementation Advise #641

Closed ghstahl closed 6 months ago

ghstahl commented 7 months ago

I want to hook any log that has a field type=audit.

func ExampleConsoleWriter() {
    log := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true})

    log.Info().Str("type", "audit").Msg("Hello World")
    // Output: <nil> INF Hello World foo=bar
}
  1. I can implement my own output, similar to ConsoleWriter, and use multi output. I look for a match and drop everything else.
  2. What else?
rs commented 7 months ago

Sorry I don't understand your request

ghstahl commented 7 months ago

I am using zerolog today to write to stdout and that gets sent to datadog.
There are certain logs events that I would like to be sent elsewhere. Not all, just ones with the type I am interested in.

So I either write a custom output based on ConsoleWriter, call it MyCustomWriter, that filters for just the ones I want and write it somewhere else (like kafka).

I was wondering if there is another way that zerolog has for me to get the full log its about to lay down.

rs commented 7 months ago

The most efficient would be to have a separate logger for those events. Note that ConsoleWriter is not meant to be used in production, you will lose of the performance benefits of this library. You could write a custom writer and parse the buffer for the type without deserializing the JSON, but it will still be quite inefficient compared to a separate logger.