Skip to content

Commit

Permalink
Merge pull request #1 from alexmt/support-custom-formatters
Browse files Browse the repository at this point in the history
feat: support using custom formatters for complex data types
  • Loading branch information
bombsimon authored Nov 3, 2020
2 parents 03a291c + ea5cad2 commit 40373e7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
36 changes: 24 additions & 12 deletions logrusr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@ const (
)

type logrusr struct {
name []string
level int
logger logrus.FieldLogger
name []string
level int
logger logrus.FieldLogger
defaultFormatter func(interface{}) string
}

// NewLogger will return a new logr.Logger from a logrus.FieldLogger.
func NewLogger(l logrus.FieldLogger, name ...string) logr.Logger {
return NewLoggerWithFormatter(l, nil, name...)
}

// NewLoggerWithFormatter will return a new logr.Logger from a
// logrus.FieldLogger that uses provided function to format complex data types.
func NewLoggerWithFormatter(l logrus.FieldLogger, formatter func(interface{}) string, name ...string) logr.Logger {
return &logrusr{
name: name,
level: 0,
logger: l,
name: name,
level: 0,
logger: l,
defaultFormatter: formatter,
}
}

Expand Down Expand Up @@ -70,7 +78,7 @@ func (l *logrusr) V(level int) logr.InfoLogger {
// will be discarded.
func (l *logrusr) WithValues(keysAndValues ...interface{}) logr.Logger {
l.logger = l.logger.WithFields(
listToLogrusFields(keysAndValues...),
listToLogrusFields(l.defaultFormatter, keysAndValues...),
)

return l
Expand All @@ -96,7 +104,7 @@ func (l *logrusr) Info(msg string, keysAndValues ...interface{}) {
}

l.logger.
WithFields(listToLogrusFields(keysAndValues...)).
WithFields(listToLogrusFields(l.defaultFormatter, keysAndValues...)).
Info(msg)
}

Expand All @@ -105,13 +113,13 @@ func (l *logrusr) Info(msg string, keysAndValues ...interface{}) {
// Error.
func (l *logrusr) Error(err error, msg string, keysAndValues ...interface{}) {
l.logger.
WithFields(listToLogrusFields(keysAndValues...)).
WithFields(listToLogrusFields(l.defaultFormatter, keysAndValues...)).
WithError(err).
Error(msg)
}

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

// Skip all fields if it's not an even lengthed list.
Expand All @@ -135,8 +143,12 @@ func listToLogrusFields(keysAndValues ...interface{}) logrus.Fields {
f[s] = string(vVal)

default:
j, _ := json.Marshal(vVal)
f[s] = string(j)
if formatter != nil {
f[s] = formatter(v)
} else {
j, _ := json.Marshal(vVal)
f[s] = string(j)
}
}
}
}
Expand Down
21 changes: 20 additions & 1 deletion logrusr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"testing"

Expand All @@ -18,6 +19,7 @@ func TestLogging(t *testing.T) {
description string
logrusLogger func() logrus.FieldLogger
logFunc func(log logr.Logger)
formatter func(interface{}) string
assertions map[string]string
}{
{
Expand Down Expand Up @@ -189,6 +191,23 @@ func TestLogging(t *testing.T) {
"list": "[1,2,3]",
},
},
{
description: "custom formatter is used",
logrusLogger: func() logrus.FieldLogger {
return logrus.New()
},
logFunc: func(log logr.Logger) {
log.Info("hello, world", "list", []int{1, 2, 3})
},
formatter: func(val interface{}) string {
return fmt.Sprintf("%v", val)
},
assertions: map[string]string{
"level": "info",
"msg": "hello, world",
"list": "[1 2 3]",
},
},
}

for _, tc := range cases {
Expand All @@ -212,7 +231,7 @@ func TestLogging(t *testing.T) {

// Send the created logger to the test case to invoke desired
// logging.
tc.logFunc(NewLogger(logrusLogger))
tc.logFunc(NewLoggerWithFormatter(logrusLogger, tc.formatter))

if tc.assertions == nil {
assert.Equal(t, logWriter.Len(), 0)
Expand Down

0 comments on commit 40373e7

Please sign in to comment.