Skip to content

first pass at implementing a zap log handler for slog #1433

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

EasterTheBunny
Copy link
Collaborator

Requires

Supports

@@ -0,0 +1,169 @@
package v2
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
package v2
package logger

Level slog.Leveler

// (optional) Logger helps convert existing zap.Logger to slog.Logger.
Logger *zap.Logger
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should target a zapcore.Core, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The only reason I used this was to allow users to drop in an existing logger. I thought it might make the conversion easier.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Which users though? I think we want to hide these details as much as possible from typical logging users. The more interesting "user" is the core logger package, which needs to use this internally to stay aligned.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I played around with using a zapcore.Core instead, but I needed to work around errors. If we use zap.Logger, we can directly pass the logger from v1 over to v2 and if it's nil we can grab a global logger with zap.L(). This allows us to return a slog.Logger without an error and cleans up the interface a little.

We can use a zapcore.Core instance instead, but our interface would then be New() (*slog.Logger, error). Maybe that's not so bad, but it would be nice to be able to drop the error handling if possible.

Comment on lines 50 to 59
if hndlr, ok := handler.(ZapHandler); ok {
return slog.New(hndlr.WithName(name))
}

return logger
}

type ZapHandler interface {
WithName(string) slog.Handler
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we could inline this and maybe keep it unexported until there is a need to expose it? I was worried more about multiple internal types than external.

Suggested change
if hndlr, ok := handler.(ZapHandler); ok {
return slog.New(hndlr.WithName(name))
}
return logger
}
type ZapHandler interface {
WithName(string) slog.Handler
}
if hndlr, ok := handler.(interface { withName(string) slog.Handler }); ok {
return slog.New(hndlr.WithName(name))
}
return logger
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That works. I had it this way to run some tests, but it's not necessary.

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.

2 participants