-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
zapslog: fix all with slogtest, support inline group, ignore empty group. #1408
Conversation
Signed-off-by: junya koyama <arukiidou@yahoo.co.jp>
Signed-off-by: junya koyama <arukiidou@yahoo.co.jp>
Signed-off-by: junya koyama <arukiidou@yahoo.co.jp>
@abhinav @tchung1118 |
Signed-off-by: junya koyama <arukiidou@yahoo.co.jp>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1408 +/- ##
==========================================
+ Coverage 98.42% 98.44% +0.01%
==========================================
Files 53 53
Lines 3495 3527 +32
==========================================
+ Hits 3440 3472 +32
Misses 46 46
Partials 9 9 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this, @arukiidou! Really appreciate the work you put into matching the slog.Handler requirements. This is looking pretty good at a high-level, but there's one issue with the state management: we can't change the handler state like that.
Once a Handler is constructed, consider all its fields immutable. Only new handlers (like the ones returned by WithAttrs) can have different values for those fields—that's the only time we can mutate them. After construction, these handlers will be re-used for concurrent log calls, so they can't modify their own state. I've left suggestions on how I think we could fix the issue.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Signed-off-by: junya koyama <arukiidou@yahoo.co.jp>
Signed-off-by: junya koyama <arukiidou@yahoo.co.jp>
58a3672
to
e10aad1
Compare
Logger name is already tested in WithName. It doesn't need to be duplicated in every test.
This requires holding onto a slice instead of a single string to create the nested namespaces. The slice is cloned on each WithGroup call. We could probably do this more efficiently with an immutable tree of references, but deferring that for now. In the process, also make the addedNamespace business append during the record.Attrs loop instead of after. This is more efficient because we don't have to shift the slice over afterwards.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the quick fix, @arukiidou!
I made a few minor fixups and changed the holdGroup
to a slice because the string can't handle cases like WithGroup("foo").WithGroup("bar").Info("msg")
.
The change looks good to me now. I'll ping a couple of the other maintainers to try to get their approval as well.
Again, thanks for the contribution!
Got an offline LGTM from @mway. |
Thanks, @arukiidou! |
This change adds a test based on testing/slogtest
that verifies compliance with the slog handler contract
(a draft of this was available in #1335),
and fixes all resulting issues.
The two remaining issues were:
Group("", attrs)
should inline the new fieldsinstead of creating a group with an empty name.
This was fixed with the use of
zap.Inline
.That is,
logger.WithGroup("foo").Info("bar")
should notcreate an empty "foo" namespace (
"foo": {}
).This was fixed by keeping track of unapplied groups
and applying them the first time a field is serialized.
Following this change, slogtest passes as expected.
Refs #1333
Resolves #1334, #1401, #1402
Supersedes #1263, #1335
TESTS