-
Notifications
You must be signed in to change notification settings - Fork 18k
log/slog: inconsistent handling of empty groups #61067
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
Comments
Thanks, I never thought about this case. I guess you could argue that a group containing another empty group isn't really empty, just like a directory containing an empty directory isn't empty. But I think your expectation is more reasonable. I'll prepare a fix. |
Change https://go.dev/cl/508436 mentions this issue: |
Change https://go.dev/cl/508438 mentions this issue: |
As #61067 pointed out, slog did not properly handle empty groups. https://go.dev/cl/508436 dealt with most cases inside slog itself, but handlers must still do a check on their own. Namely, a handler must not output a group created by WithGroup unless the Record has attributes. This change adds a test to slogtest to check that case. Fixes #61227. Change-Id: Ibc065b6e5f6e199a41bce8332ea8c7f9d8373392 Reviewed-on: https://go-review.googlesource.com/c/go/+/508438 Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Handlers should not display empty groups. A group with no attributes is certainly empty. But we also want to consider a group to be empty if all its attributes are empty groups. The built-in handlers did not handle this second case properly. This CL fixes that. There are two places in the implementation that we need to consider. For Values of KindGroup, we change the GroupValue constructor to omit Attrs that are empty groups. A Group is then empty if and only if it has no Attrs. This avoids a recursive check for emptiness. It does require allocation, but that doesn't worry us because Group values should be relatively rare. For groups established by WithGroup, we avoid opening such groups unless the Record contains non-empty groups. As we did for values, we avoid adding empty groups to records in the first place, so we only need to check that the record has at least one Attr. We are doing extra work, so we need to make sure we aren't slowing things down unduly. Benchmarks before and after this change show minimal differences. Fixes golang#61067. Change-Id: I684c7ca834bbf69210516faecae04ee548846fb7 Reviewed-on: https://go-review.googlesource.com/c/go/+/508436 Run-TryBot: Jonathan Amsterdam <jba@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
As golang#61067 pointed out, slog did not properly handle empty groups. https://go.dev/cl/508436 dealt with most cases inside slog itself, but handlers must still do a check on their own. Namely, a handler must not output a group created by WithGroup unless the Record has attributes. This change adds a test to slogtest to check that case. Fixes golang#61227. Change-Id: Ibc065b6e5f6e199a41bce8332ea8c7f9d8373392 Reviewed-on: https://go-review.googlesource.com/c/go/+/508438 Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Handlers should not display empty groups. A group with no attributes is certainly empty. But we also want to consider a group to be empty if all its attributes are empty groups. The built-in handlers did not handle this second case properly. This CL fixes that. There are two places in the implementation that we need to consider. For Values of KindGroup, we change the GroupValue constructor to omit Attrs that are empty groups. A Group is then empty if and only if it has no Attrs. This avoids a recursive check for emptiness. It does require allocation, but that doesn't worry us because Group values should be relatively rare. For groups established by WithGroup, we avoid opening such groups unless the Record contains non-empty groups. As we did for values, we avoid adding empty groups to records in the first place, so we only need to check that the record has at least one Attr. We are doing extra work, so we need to make sure we aren't slowing things down unduly. Benchmarks before and after this change show minimal differences. Fixes golang#61067. Change-Id: I684c7ca834bbf69210516faecae04ee548846fb7 Reviewed-on: https://go-review.googlesource.com/c/go/+/508436 Run-TryBot: Jonathan Amsterdam <jba@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
As golang#61067 pointed out, slog did not properly handle empty groups. https://go.dev/cl/508436 dealt with most cases inside slog itself, but handlers must still do a check on their own. Namely, a handler must not output a group created by WithGroup unless the Record has attributes. This change adds a test to slogtest to check that case. Fixes golang#61227. Change-Id: Ibc065b6e5f6e199a41bce8332ea8c7f9d8373392 Reviewed-on: https://go-review.googlesource.com/c/go/+/508438 Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Handlers should not display empty groups. A group with no attributes is certainly empty. But we also want to consider a group to be empty if all its attributes are empty groups. The built-in handlers did not handle this second case properly. This CL fixes that. There are two places in the implementation that we need to consider. For Values of KindGroup, we change the GroupValue constructor to omit Attrs that are empty groups. A Group is then empty if and only if it has no Attrs. This avoids a recursive check for emptiness. It does require allocation, but that doesn't worry us because Group values should be relatively rare. For groups established by WithGroup, we avoid opening such groups unless the Record contains non-empty groups. As we did for values, we avoid adding empty groups to records in the first place, so we only need to check that the record has at least one Attr. We are doing extra work, so we need to make sure we aren't slowing things down unduly. Benchmarks before and after this change show minimal differences. Fixes golang#61067. Change-Id: I684c7ca834bbf69210516faecae04ee548846fb7 Reviewed-on: https://go-review.googlesource.com/c/go/+/508436 Run-TryBot: Jonathan Amsterdam <jba@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
As golang#61067 pointed out, slog did not properly handle empty groups. https://go.dev/cl/508436 dealt with most cases inside slog itself, but handlers must still do a check on their own. Namely, a handler must not output a group created by WithGroup unless the Record has attributes. This change adds a test to slogtest to check that case. Fixes golang#61227. Change-Id: Ibc065b6e5f6e199a41bce8332ea8c7f9d8373392 Reviewed-on: https://go-review.googlesource.com/c/go/+/508438 Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
slog is in the next release
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
The documentation for slog.Handler.Handle states:
I expected for there to be no empty groups anywhere.
What did you see instead?
It doesn't seem to apply recursively, or apply to groups set with
WithGroup
:cc @jba
The text was updated successfully, but these errors were encountered: