Skip to content

Commit

Permalink
Adapt FormatFunc return type to the expected logrus.Fields type
Browse files Browse the repository at this point in the history
Modifies `listToLogrusFields` to use `FormatFunc`, and adapt the later to the expected logrus.Fields type

Logrus:
```go
// Fields type, used to pass to `WithFields`.
type Fields map[string]interface{}
```
https://github.com/sirupsen/logrus/blob/f8bf7650dccb756cea26edaf9217aab85500fe07/logrus.go#LL10
  • Loading branch information
d1egoaz authored and bombsimon committed Nov 25, 2022
1 parent f262fa9 commit 3576cd1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions logrusr.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const logrusDiffToInfo = 4

// FormatFunc is the function to format log values with for non primitive data.
// By default, this is empty and the data will be JSON marshaled.
type FormatFunc func(interface{}) string
type FormatFunc func(interface{}) interface{}

// Option is options to give when construction a logrusr logger.
type Option func(l *logrusr)
Expand Down Expand Up @@ -156,7 +156,7 @@ func (l *logrusr) WithName(name string) logr.LogSink {
}

// listToLogrusFields converts a list of arbitrary length to key/value paris.
func listToLogrusFields(formatter func(interface{}) string, keysAndValues ...interface{}) logrus.Fields {
func listToLogrusFields(formatter FormatFunc, keysAndValues ...interface{}) logrus.Fields {
f := make(logrus.Fields)

// Skip all fields if it's not an even length list.
Expand Down
4 changes: 2 additions & 2 deletions logrusr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestLogging(t *testing.T) {
description string
logrusLevel logrus.Level
logFunc func(log logr.Logger)
formatter func(interface{}) string
formatter FormatFunc
reportCaller bool
defaultName []string
assertions map[string]string
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestLogging(t *testing.T) {
logFunc: func(log logr.Logger) {
log.Info("hello, world", "list", []int{1, 2, 3})
},
formatter: func(val interface{}) string {
formatter: func(val interface{}) interface{} {
return fmt.Sprintf("%v", val)
},
assertions: map[string]string{
Expand Down

0 comments on commit 3576cd1

Please sign in to comment.