Skip to content

Commit

Permalink
define public interface instead use *logrus.Logger
Browse files Browse the repository at this point in the history
add example

fix lint issue with exhaustive

add new api

improve tests, format code

Update klogrus.go

Improve existing documentation

Update klogrus.go

Fix typos

Update klogrus.go

remove period
  • Loading branch information
peczenyj committed Dec 4, 2023
1 parent a6d10d4 commit 226a30d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
5 changes: 4 additions & 1 deletion plugin/klogrus/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.14.3
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // 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.6.1 // indirect
golang.org/x/sys v0.10.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions plugin/klogrus/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ github.com/twmb/franz-go/pkg/kmsg v1.6.1/go.mod h1:se9Mjdt0Nwzc9lnjJ0HyDtLyBnaBD
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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=
Expand Down
26 changes: 24 additions & 2 deletions plugin/klogrus/klogrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
Expand Down
44 changes: 44 additions & 0 deletions plugin/klogrus/klogrus_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 226a30d

Please sign in to comment.