Skip to content

Commit

Permalink
Merge pull request #470 from JGShaw/master
Browse files Browse the repository at this point in the history
Add logrus plugin
  • Loading branch information
twmb authored Jul 8, 2023
2 parents 6fb7260 + 3fb37c7 commit 118e822
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Each of these plugins has a corresponding example in [examples](../examples/hook
<a href="./">plugin</a> — you are here
├── <a href="./kgmetrics">kgmetrics</a> — plug-in go-metrics to use with `kgo.WithHooks`
├── <a href="./kprom">kprom</a> — plug-in prometheus metrics to use with `kgo.WithHooks`
├── <a href="./klogrus">klogrus</a> — plug-in sirupsen/logrus to use with `kgo.WithLogger`
├── <a href="./kzap">kzap</a> — plug-in uber-go/zap to use with `kgo.WithLogger`
└── <a href="./kzerolog">kzerolog</a> — plug-in rs/zerolog to use with `kgo.WithLogger`
</pre>
13 changes: 13 additions & 0 deletions plugin/klogrus/README.md
Original file line number Diff line number Diff line change
@@ -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
)
```
16 changes: 16 additions & 0 deletions plugin/klogrus/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
27 changes: 27 additions & 0 deletions plugin/klogrus/go.sum
Original file line number Diff line number Diff line change
@@ -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=
63 changes: 63 additions & 0 deletions plugin/klogrus/klogrus.go
Original file line number Diff line number Diff line change
@@ -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.GetLevel())
}

// Log is for the kgo.Logger interface.
func (l *Logger) Log(level kgo.LogLevel, msg string, keyvals ...any) {
logrusLevel, levelMatched := kgoToLogrusLevel(level)
if levelMatched {
fields := make(logrus.Fields, len(keyvals)/2)
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
}
}

0 comments on commit 118e822

Please sign in to comment.