Skip to content

Commit

Permalink
Merge pull request #596 from sbuliarca/slog-logger-plugin
Browse files Browse the repository at this point in the history
feat(kslog): implemented kgo.Logger plugin with slog
  • Loading branch information
twmb authored Oct 22, 2023
2 parents 019f134 + 70fdfe1 commit 88b2a86
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
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:
}

0 comments on commit 88b2a86

Please sign in to comment.