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

log/slog: drop only completely empty Attrs #59282

Closed
jba opened this issue Mar 27, 2023 · 5 comments
Closed

log/slog: drop only completely empty Attrs #59282

jba opened this issue Mar 27, 2023 · 5 comments

Comments

@jba
Copy link
Contributor

jba commented Mar 27, 2023

Currently, handlers are expected to drop attributes with an empty key (except for Groups), and the built-in handlers do so. I propose changing that convention: only an Attr whose key is empty and whose value is nil should be dropped.

@ianthehat pointed out to me that omitting the key but not the value could indicate a bug (for example, by using a variable for the key that unintentionally holds the empty string). One of slog's tenets is to display whatever it can—that's why we display mismatched key-value pairs instead of dropping them or failing—and the same argument applies here.

Most uses of this feature are probably in ReplaceAttr functions, where it is easy to make this change.

Handlers can detect an empty Attr a with a.Equal(Attr{}).

See also #56345.

@jba jba added the Proposal label Mar 27, 2023
@gopherbot gopherbot added this to the Proposal milestone Mar 27, 2023
@rsc
Copy link
Contributor

rsc commented Mar 29, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@ChrisHines
Copy link
Contributor

I think this is a good idea. After the decision was made to interpret an empty key as a "skip" attr I was wondering if that would cause problems with the Attr.Equal method. because two Attrs with empty keys but different values would not be equal but would behave the same in terms of logging. That usually wouldn't matter, but there are use cases for testing where a Hander simply records in memory each Record that gets logged so that the test can validate specific things are getting logged. The validation code would likely make heavy use of Attr.Equal and two "skip" attrs that didn't compare equal would be confusing. The Equal method could be implemented to ignore the value when the keys are both empty, but I think this change is cleaner and I can't think of any loss in functionality.

@rsc
Copy link
Contributor

rsc commented Apr 6, 2023

Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group

@gopherbot
Copy link

Change https://go.dev/cl/484095 mentions this issue: log/slog: require entire Attr to be empty to elide

@rsc
Copy link
Contributor

rsc commented Apr 12, 2023

No change in consensus, so accepted. 🎉
This issue now tracks the work of implementing the proposal.
— rsc for the proposal review group

@rsc rsc changed the title proposal: log/slog: drop only completely empty Attrs log/slog: drop only completely empty Attrs Apr 12, 2023
@rsc rsc modified the milestones: Proposal, Backlog Apr 12, 2023
@dmitshur dmitshur modified the milestones: Backlog, Go1.21 Jun 4, 2023
eric pushed a commit to fancybits/go that referenced this issue Sep 7, 2023
Specify that Handlers should ignore zero-valued Attrs.

Implement that policy in the built-in handlers.

Fixes golang#59282.

Change-Id: I4430686b61f49bdac849ee300daaabfac9895849
Reviewed-on: https://go-review.googlesource.com/c/go/+/484095
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
eric pushed a commit to fancybits/go that referenced this issue Sep 7, 2023
Specify that Handlers should ignore zero-valued Attrs.

Implement that policy in the built-in handlers.

Fixes golang#59282.

Change-Id: I4430686b61f49bdac849ee300daaabfac9895849
Reviewed-on: https://go-review.googlesource.com/c/go/+/484095
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants