-
Notifications
You must be signed in to change notification settings - Fork 602
Description
Hello!
I've developed a (soon to be OSS) "unified" logging interface/library somewhat analogous to database/sql where you import drivers; zerolog is one such driver. I'll be happy to add it to 'who uses zerolog' once it is available!
The interface defines some things that are not readily available in zerolog. Such as being able to change the event level after it is created; I know this is in contrast to zerolog that wants to define the level initially for reasons such as enabling/disabling/nil-ing out the event for performance and other reasons. The interface also allows you to delay when the event is written.
Both of these are primarily to support canonical log lines, and some other features.
Currently the driver gets around the event level issue by creating the initial event with 'NoLevel', this makes it valid from zerologs perspective and essentially disables zerolog from writing the level itself and allows me to write the level later when known.
This causes an issue with the hook interface, though. While the hooks will still be called they are all fed 'NoLevel' instead of the level that is known at the time Msg is called. I don't see a way to access the event level itself (#252) or access the hooks that have been set so I can call them with the correct level. I see that #255 hasn't been accepted, in part because it breaks code that writes the level first, but would also panic in code that had a disabled level (nil event) to begin with.
Considering using 'NoLevel' is a valid use case with zerolog, and writing the level yourself is easily accomplished with the same code found here I wonder if a modification of #255 would work. Allow changing the event as long as the event is non-nil and the event is coming from 'NoLevel' or the level changing remains 'enabled'. Meaning you can go from enabled->enabled but not disabled->enabled.
Something along the lines of
func (e *Event) Level(level Level) *Event {
// perhaps leaving off the NoLevel check
if e.Enabled() && e.level == NoLevel {
e.level = level
}
return e
}I would be happy to provide a PR for this, or one for an alternative solution (to the event level and/or hooks).
PS: I have a tangential issue regarding retaining zerologs performance in this driver that I'd like to discuss, I can create a new issue for it or discuss it here if you don't mind. Let me know.
Thank you!