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

feat(kslog): implemented kgo.Logger plugin with slog #596

Merged
merged 2 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions plugin/kslog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
kslog
===

kslog is a plug-in package to use [slog](https://pkg.go.dev/log/slog) as a [`kgo.Logger`](https://pkg.go.dev/github.com/twmb/franz-go/pkg/kgo#Logger)

To use,

```go
cl, err := kgo.NewClient(
kgo.WithLogger(kslog.New(slog.Default())),
// ...other opts
)
```
11 changes: 11 additions & 0 deletions plugin/kslog/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/twmb/franz-go/plugin/kslog

go 1.21.0

require github.com/twmb/franz-go v1.15.0

require (
github.com/klauspost/compress v1.16.7 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/twmb/franz-go/pkg/kmsg v1.6.1 // indirect
)
8 changes: 8 additions & 0 deletions plugin/kslog/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ=
github.com/pierrec/lz4/v4 v4.1.18/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/twmb/franz-go v1.15.0 h1:bw5n1COKJzWpkCXG/kMtHrurcS9HSWV6e3If5CUdc+M=
github.com/twmb/franz-go v1.15.0/go.mod h1:nMAvTC2kHtK+ceaSHeHm4dlxC78389M/1DjpOswEgu4=
github.com/twmb/franz-go/pkg/kmsg v1.6.1 h1:tm6hXPv5antMHLasTfKv9R+X03AjHSkSkXhQo2c5ALM=
github.com/twmb/franz-go/pkg/kmsg v1.6.1/go.mod h1:se9Mjdt0Nwzc9lnjJ0HyDtLyBnaBDAd7pCje47OhSyw=
66 changes: 66 additions & 0 deletions plugin/kslog/kslog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Package kslog provides a plug-in kgo.Logger wrapping slog.Logger for usage in
// a kgo.Client.
//
// This can be used like so:
//
// cl, err := kgo.NewClient(
// kgo.WithLogger(kslog.New(slog.Default())),
// // ...other opts
// )
package kslog

import (
"context"
"log/slog"

"github.com/twmb/franz-go/pkg/kgo"
)

// Logger provides the kgo.Logger interface for usage in kgo.WithLogger when
// initializing a client.
type Logger struct {
sl *slog.Logger
}

// New returns a new kgo.Logger that wraps an slog.Logger.
func New(sl *slog.Logger) *Logger {
return &Logger{sl}
}

// Level is for the kgo.Logger interface.
func (l *Logger) Level() kgo.LogLevel {
ctx := context.Background()
switch {
case l.sl.Enabled(ctx, slog.LevelDebug):
return kgo.LogLevelDebug
case l.sl.Enabled(ctx, slog.LevelInfo):
return kgo.LogLevelInfo
case l.sl.Enabled(ctx, slog.LevelWarn):
return kgo.LogLevelWarn
case l.sl.Enabled(ctx, slog.LevelError):
return kgo.LogLevelError
default:
return kgo.LogLevelNone
}
}

// Log is for the kgo.Logger interface.
func (l *Logger) Log(level kgo.LogLevel, msg string, keyvals ...any) {
l.sl.Log(context.Background(), kgoToSlogLevel(level), msg, keyvals...)
}

func kgoToSlogLevel(level kgo.LogLevel) slog.Level {
switch level {
case kgo.LogLevelError:
return slog.LevelError
case kgo.LogLevelWarn:
return slog.LevelWarn
case kgo.LogLevelInfo:
return slog.LevelInfo
case kgo.LogLevelDebug:
return slog.LevelDebug
default:
// Using the default level for slog
return slog.LevelInfo
}
}
15 changes: 15 additions & 0 deletions plugin/kslog/kslog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kslog_test

import (
"log/slog"

"github.com/twmb/franz-go/pkg/kgo"
"github.com/twmb/franz-go/plugin/kslog"
)

func ExampleNew() {
l := kslog.New(slog.Default())

l.Log(kgo.LogLevelInfo, "test message", "test-key", "test-val")
// Output:
}