From 94e205f90609584fb3f48788f600739f199395ec Mon Sep 17 00:00:00 2001 From: Joseph Shaw Date: Wed, 7 Jun 2023 16:46:12 +0100 Subject: [PATCH 1/3] Add logging plugin for sirupsen/logrus Add logrus loggings plugin Update readme with details of logrus plugin --- plugin/README.md | 1 + plugin/klogrus/README.md | 13 ++++++++ plugin/klogrus/go.mod | 16 ++++++++++ plugin/klogrus/go.sum | 27 +++++++++++++++++ plugin/klogrus/klogrus.go | 63 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 plugin/klogrus/README.md create mode 100644 plugin/klogrus/go.mod create mode 100644 plugin/klogrus/go.sum create mode 100644 plugin/klogrus/klogrus.go diff --git a/plugin/README.md b/plugin/README.md index 1e835feb..fc0c9581 100644 --- a/plugin/README.md +++ b/plugin/README.md @@ -13,6 +13,7 @@ Each of these plugins has a corresponding example in [examples](../examples/hook plugin — you are here ├── kgmetrics — plug-in go-metrics to use with `kgo.WithHooks` ├── kprom — plug-in prometheus metrics to use with `kgo.WithHooks` +├── klogrus — plug-in sirupsen/logrus to use with `kgo.WithLogger` ├── kzap — plug-in uber-go/zap to use with `kgo.WithLogger` └── kzerolog — plug-in rs/zerolog to use with `kgo.WithLogger` diff --git a/plugin/klogrus/README.md b/plugin/klogrus/README.md new file mode 100644 index 00000000..47b33bfb --- /dev/null +++ b/plugin/klogrus/README.md @@ -0,0 +1,13 @@ +klogrus +=== + +klogrus is a plug-in package to use [logrus](https://github.com/sirupsen/logrus) 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(klogrus.New(logrusLogger)), + // ...other opts +) +``` \ No newline at end of file diff --git a/plugin/klogrus/go.mod b/plugin/klogrus/go.mod new file mode 100644 index 00000000..fb5cf51f --- /dev/null +++ b/plugin/klogrus/go.mod @@ -0,0 +1,16 @@ +module github.com/twmb/franz-go/plugin/klogrus + +go 1.18 + +require ( + github.com/sirupsen/logrus v1.9.3 + github.com/twmb/franz-go v1.13.5 +) + +require ( + github.com/klauspost/compress v1.16.3 // indirect + github.com/pierrec/lz4/v4 v4.1.17 // indirect + github.com/stretchr/testify v1.8.0 // indirect + github.com/twmb/franz-go/pkg/kmsg v1.4.0 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +) diff --git a/plugin/klogrus/go.sum b/plugin/klogrus/go.sum new file mode 100644 index 00000000..fa9da63b --- /dev/null +++ b/plugin/klogrus/go.sum @@ -0,0 +1,27 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY= +github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc= +github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/twmb/franz-go v1.13.5 h1:7Hk47eZ7XRb4yWXQZk1GZU4BthkrKuZUfKOuP9Sgp24= +github.com/twmb/franz-go v1.13.5/go.mod h1:jm/FtYxmhxDTN0gNSb26XaJY0irdSVcsckLiR5tQNMk= +github.com/twmb/franz-go/pkg/kmsg v1.4.0 h1:tbp9hxU6m8qZhQTlpGiaIJOm4BXix5lsuEZ7K00dF0s= +github.com/twmb/franz-go/pkg/kmsg v1.4.0/go.mod h1:SxG/xJKhgPu25SamAq0rrucfp7lbzCpEXOC+vH/ELrY= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +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= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/plugin/klogrus/klogrus.go b/plugin/klogrus/klogrus.go new file mode 100644 index 00000000..73fbda80 --- /dev/null +++ b/plugin/klogrus/klogrus.go @@ -0,0 +1,63 @@ +package klogrus + +import ( + "github.com/sirupsen/logrus" + "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 { + lr *logrus.Logger +} + +// New returns a new Logger. +func New(lr *logrus.Logger) *Logger { + return &Logger{lr} +} + +// Level is for the kgo.Logger interface. +func (l *Logger) Level() kgo.LogLevel { + return logrusToKgoLevel(l.lr.Level) +} + +// Log is for the kgo.Logger interface. +func (l *Logger) Log(level kgo.LogLevel, msg string, keyvals ...any) { + logrusLevel, levelMatched := kgoToLogrusLevel(level) + if levelMatched { + var fields logrus.Fields + for i := 0; i < len(keyvals); i += 2 { + fields[keyvals[i].(string)] = keyvals[i+1] + } + l.lr.WithFields(fields).Log(logrusLevel, msg) + } +} + +func kgoToLogrusLevel(level kgo.LogLevel) (logrus.Level, bool) { + switch level { + case kgo.LogLevelError: + return logrus.ErrorLevel, true + case kgo.LogLevelWarn: + return logrus.WarnLevel, true + case kgo.LogLevelInfo: + return logrus.InfoLevel, true + case kgo.LogLevelDebug: + return logrus.DebugLevel, true + } + return logrus.TraceLevel, false +} + +func logrusToKgoLevel(level logrus.Level) kgo.LogLevel { + switch level { + case logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel: + return kgo.LogLevelError + case logrus.WarnLevel: + return kgo.LogLevelWarn + case logrus.InfoLevel: + return kgo.LogLevelInfo + case logrus.DebugLevel, logrus.TraceLevel: + return kgo.LogLevelDebug + default: + return kgo.LogLevelNone + } +} From e3f18bdce531751599f03bb70d866af22ef8beec Mon Sep 17 00:00:00 2001 From: Joseph Shaw Date: Tue, 27 Jun 2023 09:03:23 +0100 Subject: [PATCH 2/3] Use GetLevel instead of readching into the struct --- plugin/klogrus/klogrus.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/klogrus/klogrus.go b/plugin/klogrus/klogrus.go index 73fbda80..d8f6c391 100644 --- a/plugin/klogrus/klogrus.go +++ b/plugin/klogrus/klogrus.go @@ -18,7 +18,7 @@ func New(lr *logrus.Logger) *Logger { // Level is for the kgo.Logger interface. func (l *Logger) Level() kgo.LogLevel { - return logrusToKgoLevel(l.lr.Level) + return logrusToKgoLevel(l.lr.GetLevel()) } // Log is for the kgo.Logger interface. From 3fb37c710f6240c34590a394b086ce682b1ee735 Mon Sep 17 00:00:00 2001 From: Joseph Shaw Date: Tue, 27 Jun 2023 09:05:19 +0100 Subject: [PATCH 3/3] Initialize the empty map of fields correctly so that the map is not nil --- plugin/klogrus/klogrus.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/klogrus/klogrus.go b/plugin/klogrus/klogrus.go index d8f6c391..207c3743 100644 --- a/plugin/klogrus/klogrus.go +++ b/plugin/klogrus/klogrus.go @@ -25,7 +25,7 @@ func (l *Logger) Level() kgo.LogLevel { func (l *Logger) Log(level kgo.LogLevel, msg string, keyvals ...any) { logrusLevel, levelMatched := kgoToLogrusLevel(level) if levelMatched { - var fields logrus.Fields + fields := make(logrus.Fields, len(keyvals)/2) for i := 0; i < len(keyvals); i += 2 { fields[keyvals[i].(string)] = keyvals[i+1] }