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

slogr: add SlogSink #211

Merged
merged 1 commit into from Aug 27, 2023
Merged

slogr: add SlogSink #211

merged 1 commit into from Aug 27, 2023

Conversation

pohly
Copy link
Contributor

@pohly pohly commented Aug 25, 2023

A LogSink may implement this additional interface if it wants to support logging through slog better. Doing so addresses several issues:

  • stack unwinding gets avoided in favor of logging the pre-recorded PC
  • proper grouping of key/value pairs via WithGroup
  • verbosity levels > slog.LevelInfo can be recorded
  • less overhead

A LogSink implementation which wants to provide a New*Handler() slog.Handler function can either implement a second type (required because the prototype of the Enabled method conflicts) or use logr.NewSlogHandler. The latter is better because then conversion back-and-forth works. The overhead for the additional wrapper should be minimal.

@pohly
Copy link
Contributor Author

pohly commented Aug 25, 2023

See go-logr/zapr#60 for a proper implementation of the proposed SlogSink.

@pohly pohly mentioned this pull request Aug 25, 2023
4 tasks
Copy link
Contributor

@thockin thockin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super small nits. I get it now. I wonder if we should explain somewhere why this is like this. Specifically, that Enabled is in both logr.Logger and slog.Handler interfaces. This design allows people who already have a logr.Logger to use slog-aware implementations and slog-unaware implementations in the exact same way, without sacrificing functionality.


// slogSink is only used through slog and thus doesn't need to implement the
// normal LogSink methods.
type slogSink struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this to include "test" so it is less ambiguous wrt SlogSink

slogr/slogr.go Outdated
return handler
}

// SlogSink is an interface that a LogSink can implement to support logging
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...is an optional interface...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

I wonder if we should explain somewhere why this is like this.

I added more comments to the SlogSink interface about this.

A LogSink may implement this additional interface if it wants to support
logging through slog better. Doing so addresses several issues:

- stack unwinding gets avoided in favor of logging the pre-recorded PC
- proper grouping of key/value pairs via WithGroup
- verbosity levels > slog.LevelInfo can be recorded
- less overhead

A LogSink implementation which wants to provide a `New*Handler() slog.Handler`
function can either implement a second type (required because the prototype of
the Enabled method conflicts) or use `logr.NewSlogHandler`. The latter is
better because then conversion back-and-forth works. The overhead for the
additional wrapper should be minimal.
@thockin thockin merged commit 7bf6c82 into go-logr:master Aug 27, 2023
14 checks passed
@thockin
Copy link
Contributor

thockin commented Aug 27, 2023

LGTM

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.

None yet

2 participants