rs / zerolog

Zero Allocation JSON Logger
MIT License
10.61k stars 572 forks source link

Add EmptyFields method to remove all the fileds from logger #575

Closed GRbit closed 8 months ago

GRbit commented 1 year ago

Hello dear zerolog maintainers! First of all, thanks for the great logging package, which is also the fastest logger available. Hats off to everyone who worked on it.

This feature adds the ability to clear all previously added fields when creating a new logger.

Sometimes, when I pass the logger from one part of my service to another, I want to keep all the logger settings, but remove all the fields. For example, I can use the same field name to identify the context of the logger. If I reuse it, the result will have many fields with the same name, which doesn't look good: {"level":"info","context":"internal-logic","context":"db-adapter","context":"db-driver","message":"something"}

Using this method, I can create a new logger instance like this: log.With().EmptyFields().Str("context","db-driver").Logger() And later have messages with only one "context" field: {"level":"info","context":"db-driver","message":"something"}

GRbit commented 1 year ago

@rs sorry to bother you, but can you give me your brief opinion on the idea? Like, do you think it's a nice feature and will review it one day in the future, or do you have doubts about whether it's needed or not and I should probably just use a fork of mine?

rs commented 8 months ago

What about renaming this method Reset?

madkins23 commented 8 months ago

Upvote from me, I have a use for this. I might suggest ResetContext as a rename but that's just overly picky. ;-)

GRbit commented 8 months ago

Thank you very much for the thorough review @rs ! I agree with all your suggestions, Reset indeed looks like a nice generic name (as well as ResetContext).

P.S.: it's very nice of you to highlight all the places where the function should be renamed, it just took me a few clicks.