rs / zerolog

Zero Allocation JSON Logger
MIT License
10.41k stars 567 forks source link

Add Event.Size() method to return the size of the event buffer #535

Closed bderrly closed 1 year ago

bderrly commented 1 year ago

I am writing a hook to increment some opentelemetry metrics in a logging library. One of the things I would like to count is the sum of bytes written. I can easily get the count of bytes for the "message" (as a parameter to the hook callback) but I want the entire event that will be written.

I wasn't sure if it would be preferable to return a zero or -1 in the case of a nil Event. Thoughts?

rs commented 1 year ago

Some other hooks after yours could add bytes and the closing of the event will add some too. I would suggest to use a io.Writer wrapper on the output to log event size. Each call to write is guaranteed to be a single full event.