zerolog icon indicating copy to clipboard operation
zerolog copied to clipboard

Add EmptyFields method to remove all the fileds from logger

Open GRbit opened this issue 2 years ago • 1 comments

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 avatar Aug 09 '23 07:08 GRbit

@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?

GRbit avatar Sep 06 '23 12:09 GRbit

What about renaming this method Reset?

rs avatar Mar 02 '24 00:03 rs

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

madkins23 avatar Mar 02 '24 16:03 madkins23

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.

GRbit avatar Mar 04 '24 09:03 GRbit