-
Notifications
You must be signed in to change notification settings - Fork 18
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
integrate with log/slog #35
Comments
It's not clear to me how to build a Go kit Logger that wraps a slog.Handler. I hope you can provide some guidance! I guess it would have to look something like the following. type slogLogger struct {
handler slog.Handler
}
func NewSlogLogger(h slog.Handler) Logger {
return &slogLogger{handler: h}
}
func (sl *slogLogger) Log(keyvals ...any) error {
ctx := context.Background() // I think this is right
rec := ???
return sl.handler.Handle(ctx, rec)
} What isn't clear to me is how to construct a slog.Record. The slog.NewRecord constructor takes
Looking forward to any thoughts you might have. |
I'm not too worried about getting the mapping perfect, or even great. I think there will be a lot of value in just having the code in your repo, so go-kit/log users will see how it's done. For example, if someone consistently uses "message" instead of "msg", they can copy your code, or maybe suggest adding an option to |
What about wrapping the type logFunc func(msg string, keysAndValues ...interface{})
func (l logFunc) Log(keyvals ...interface{}) error {
l("", keyvals...)
return nil
}
type config struct {
level slog.Level
}
type Option func(*config)
func WithLevel(level slog.Level) Option {
return func(l *config) {
l.level = level
}
}
func NewSlogLogger(logger *slog.Logger, options ...Option) kitlog.Logger {
c := &config{}
for _, option := range options {
option(c)
}
var logFunc logFunc
switch c.level {
case slog.LevelDebug:
logFunc = slog.Debug
case slog.LevelInfo:
logFunc = slog.Info
case slog.LevelWarn:
logFunc = slog.Warn
case slog.LevelError:
logFunc = slog.Error
default:
logFunc = slog.Info
}
return logFunc
} |
example with log/slog: https://github.com/isauran/logger @xyluet thank you! |
If anyone is interested, I put together an adapter from go-kit/log to slog, for Grafana Mimir. I can't guarantee it works perfectly (especially uncertain about |
@aknuds1 and I have extracted the adapter out of mimir and generalized it into a standalone library. Hope it helps 👍 https://github.com/tjhop/slog-gokit |
Thanks, adding it to https://go.dev/wiki/Resources-for-slog. |
The
log/slog
package will be in Go 1.21.Consider adding a Logger that wraps a
slog.Handler
.The text was updated successfully, but these errors were encountered: