From 8e2e68f28af0b263786c7ad5cc6461e24b9803e0 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 21 Dec 2023 13:13:46 +0100 Subject: [PATCH] fresh restart --- plugin/klogrus/go.mod | 5 +++- plugin/klogrus/go.sum | 1 + plugin/klogrus/klogrus.go | 26 ++++++++++++++++++-- plugin/klogrus/klogrus_test.go | 44 ++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 plugin/klogrus/klogrus_test.go diff --git a/plugin/klogrus/go.mod b/plugin/klogrus/go.mod index 690af8ba..aec8b14d 100644 --- a/plugin/klogrus/go.mod +++ b/plugin/klogrus/go.mod @@ -4,13 +4,16 @@ go 1.18 require ( github.com/sirupsen/logrus v1.9.3 + github.com/stretchr/testify v1.8.0 github.com/twmb/franz-go v1.15.3 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/klauspost/compress v1.17.4 // indirect github.com/pierrec/lz4/v4 v4.1.19 // indirect - github.com/stretchr/testify v1.8.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/twmb/franz-go/pkg/kmsg v1.7.0 // indirect golang.org/x/sys v0.15.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/plugin/klogrus/go.sum b/plugin/klogrus/go.sum index c4cbc72a..6eea62ef 100644 --- a/plugin/klogrus/go.sum +++ b/plugin/klogrus/go.sum @@ -22,6 +22,7 @@ github.com/twmb/franz-go/pkg/kmsg v1.7.0/go.mod h1:se9Mjdt0Nwzc9lnjJ0HyDtLyBnaBD golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/plugin/klogrus/klogrus.go b/plugin/klogrus/klogrus.go index 70ec6364..da10380a 100644 --- a/plugin/klogrus/klogrus.go +++ b/plugin/klogrus/klogrus.go @@ -7,17 +7,37 @@ import ( "github.com/twmb/franz-go/pkg/kgo" ) +var ( + _ FieldLogger = (*logrus.Logger)(nil) + _ kgo.Logger = (*Logger)(nil) +) + +// FieldLogger interface combines logrus.FieldLogger with GetLevel method +// useful to represent a wrapper around *logrus.Logger. +type FieldLogger interface { + logrus.FieldLogger + GetLevel() logrus.Level +} + // Logger provides the kgo.Logger interface for usage in kgo.WithLogger when // initializing a client. type Logger struct { - lr *logrus.Logger + lr FieldLogger } -// New returns a new Logger. +// New returns a new Logger using a *logrus.Logger instance. func New(lr *logrus.Logger) *Logger { return &Logger{lr} } +// NewFieldLogger returns a new Logger using a FieldLogger interface. +// it is isofunctional with New constructor, except it can accept either +// *logrus.Logger or a possible wrapper that implements logrus.FieldLogger +// and includes GetLevel method. +func NewFieldLogger(fl FieldLogger) *Logger { + return &Logger{fl} +} + // Level is for the kgo.Logger interface. func (l *Logger) Level() kgo.LogLevel { return logrusToKgoLevel(l.lr.GetLevel()) @@ -45,6 +65,8 @@ func kgoToLogrusLevel(level kgo.LogLevel) (logrus.Level, bool) { return logrus.InfoLevel, true case kgo.LogLevelDebug: return logrus.DebugLevel, true + case kgo.LogLevelNone: + return logrus.TraceLevel, false } return logrus.TraceLevel, false } diff --git a/plugin/klogrus/klogrus_test.go b/plugin/klogrus/klogrus_test.go new file mode 100644 index 00000000..1aba950d --- /dev/null +++ b/plugin/klogrus/klogrus_test.go @@ -0,0 +1,44 @@ +package klogrus_test + +import ( + "testing" + + "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus/hooks/test" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/twmb/franz-go/pkg/kgo" + "github.com/twmb/franz-go/plugin/klogrus" +) + +func ExampleNew() { + l := klogrus.New(logrus.New()) + + l.Log(kgo.LogLevelInfo, "test message", "test-key", "test-val") + // Output: +} + +func TestFieldLogger(t *testing.T) { + logger, hook := test.NewNullLogger() + + l := klogrus.NewFieldLogger(logger) + + level := l.Level() + assert.Equal(t, kgo.LogLevelInfo, level) + + l.Log(kgo.LogLevelInfo, "test message", "test-key", "test-val") + + require.Equal(t, 1, len(hook.Entries)) + lastEntry := hook.LastEntry() + + assert.Equal(t, logrus.InfoLevel, lastEntry.Level) + assert.Equal(t, "test message", lastEntry.Message) + + value, ok := lastEntry.Data["test-key"] + assert.True(t, ok) + assert.Equal(t, "test-val", value) + + hook.Reset() + assert.Nil(t, hook.LastEntry()) +}