Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Mar 6, 2024

Conversation

GRbit
Copy link
Contributor

@GRbit GRbit commented Aug 9, 2023

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
Copy link
Contributor Author

GRbit commented Sep 6, 2023

@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
Copy link
Owner

rs commented Mar 2, 2024

What about renaming this method Reset?

@madkins23
Copy link
Contributor

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

.gitignore Outdated Show resolved Hide resolved
context.go Outdated Show resolved Hide resolved
log_test.go Outdated Show resolved Hide resolved
log_test.go Outdated Show resolved Hide resolved
GRbit and others added 4 commits March 4, 2024 09:45
@GRbit
Copy link
Contributor Author

GRbit commented Mar 4, 2024

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.

context.go Outdated Show resolved Hide resolved
@rs rs merged commit 74cf37a into rs:master Mar 6, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants