From 3374c3339c116b0168847aa7fead4fdaaea97ba8 Mon Sep 17 00:00:00 2001 From: Sorin Buliarca Date: Fri, 13 Oct 2023 12:38:12 +0300 Subject: [PATCH 1/2] feat(kslog): implemented kgo.Logger plugin with slog --- plugin/kslog/README.md | 13 ++++++++ plugin/kslog/go.mod | 11 +++++++ plugin/kslog/go.sum | 8 +++++ plugin/kslog/kslog.go | 66 ++++++++++++++++++++++++++++++++++++++ plugin/kslog/kslog_test.go | 14 ++++++++ 5 files changed, 112 insertions(+) create mode 100644 plugin/kslog/README.md create mode 100644 plugin/kslog/go.mod create mode 100644 plugin/kslog/go.sum create mode 100644 plugin/kslog/kslog.go create mode 100644 plugin/kslog/kslog_test.go diff --git a/plugin/kslog/README.md b/plugin/kslog/README.md new file mode 100644 index 00000000..b51a44bf --- /dev/null +++ b/plugin/kslog/README.md @@ -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 +) +``` diff --git a/plugin/kslog/go.mod b/plugin/kslog/go.mod new file mode 100644 index 00000000..0356b1b0 --- /dev/null +++ b/plugin/kslog/go.mod @@ -0,0 +1,11 @@ +module github.com/utilitywarehouse/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 +) diff --git a/plugin/kslog/go.sum b/plugin/kslog/go.sum new file mode 100644 index 00000000..9b32193a --- /dev/null +++ b/plugin/kslog/go.sum @@ -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= diff --git a/plugin/kslog/kslog.go b/plugin/kslog/kslog.go new file mode 100644 index 00000000..d3389665 --- /dev/null +++ b/plugin/kslog/kslog.go @@ -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 + } +} diff --git a/plugin/kslog/kslog_test.go b/plugin/kslog/kslog_test.go new file mode 100644 index 00000000..4c84d027 --- /dev/null +++ b/plugin/kslog/kslog_test.go @@ -0,0 +1,14 @@ +package kslog + +import ( + "log/slog" + "testing" + + "github.com/twmb/franz-go/pkg/kgo" +) + +func TestNew(_ *testing.T) { + l := New(slog.Default()) + + l.Log(kgo.LogLevelInfo, "test message", "test-key", "test-val") +} From 70fdfe1f294fb64a412319ed8fbe265e0aaaced1 Mon Sep 17 00:00:00 2001 From: Sorin Buliarca Date: Mon, 16 Oct 2023 09:47:43 +0300 Subject: [PATCH 2/2] feat(kslog): fixed module & use example instead of test to demonstrate usage --- plugin/kslog/go.mod | 2 +- plugin/kslog/kslog_test.go | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/plugin/kslog/go.mod b/plugin/kslog/go.mod index 0356b1b0..5e0ab27b 100644 --- a/plugin/kslog/go.mod +++ b/plugin/kslog/go.mod @@ -1,4 +1,4 @@ -module github.com/utilitywarehouse/franz-go/plugin/kslog +module github.com/twmb/franz-go/plugin/kslog go 1.21.0 diff --git a/plugin/kslog/kslog_test.go b/plugin/kslog/kslog_test.go index 4c84d027..54371387 100644 --- a/plugin/kslog/kslog_test.go +++ b/plugin/kslog/kslog_test.go @@ -1,14 +1,15 @@ -package kslog +package kslog_test import ( "log/slog" - "testing" "github.com/twmb/franz-go/pkg/kgo" + "github.com/twmb/franz-go/plugin/kslog" ) -func TestNew(_ *testing.T) { - l := New(slog.Default()) +func ExampleNew() { + l := kslog.New(slog.Default()) l.Log(kgo.LogLevelInfo, "test message", "test-key", "test-val") + // Output: }