Skip to content

Commit

Permalink
Rename defaultFormatter to formatter - it's the only one
Browse files Browse the repository at this point in the history
The field is not the default formatter, it's the actual formatter set by
the user. The default formatter is used when this is empty and that is
to just JSON marshal the type.
  • Loading branch information
bombsimon committed Nov 26, 2022
1 parent af29dd7 commit d198d3e
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions logrusr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
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.
// If this is not set (default) all unknown types will be JSON marshaled and
// added as a string.
type FormatFunc func(interface{}) interface{}

// Option is options to give when construction a logrusr logger.
Expand All @@ -27,7 +28,7 @@ type Option func(l *logrusr)
// WithFormatter will set the FormatFunc to use.
func WithFormatter(f FormatFunc) Option {
return func(l *logrusr) {
l.defaultFormatter = f
l.formatter = f
}
}

Expand All @@ -51,11 +52,11 @@ func WithName(name ...string) Option {
}

type logrusr struct {
name []string
depth int
reportCaller bool
logger *logrus.Entry
defaultFormatter FormatFunc
name []string
depth int
reportCaller bool
logger *logrus.Entry
formatter FormatFunc
}

// New will return a new logr.Logger created from a logrus.FieldLogger.
Expand Down Expand Up @@ -110,7 +111,7 @@ func (l *logrusr) Info(level int, msg string, keysAndValues ...interface{}) {
}

log.
WithFields(listToLogrusFields(l.defaultFormatter, keysAndValues...)).
WithFields(listToLogrusFields(l.formatter, keysAndValues...)).
Log(logrus.Level(level+logrusDiffToInfo), msg)
}

Expand All @@ -124,7 +125,7 @@ func (l *logrusr) Error(err error, msg string, keysAndValues ...interface{}) {
}

log.
WithFields(listToLogrusFields(l.defaultFormatter, keysAndValues...)).
WithFields(listToLogrusFields(l.formatter, keysAndValues...)).
WithError(err).
Error(msg)
}
Expand All @@ -136,7 +137,7 @@ func (l *logrusr) Error(err error, msg string, keysAndValues ...interface{}) {
func (l *logrusr) WithValues(keysAndValues ...interface{}) logr.LogSink {
newLogger := l.copyLogger()
newLogger.logger = newLogger.logger.WithFields(
listToLogrusFields(l.defaultFormatter, keysAndValues...),
listToLogrusFields(l.formatter, keysAndValues...),
)

return newLogger
Expand Down Expand Up @@ -197,11 +198,11 @@ func listToLogrusFields(formatter FormatFunc, keysAndValues ...interface{}) logr
// the formatter and actual logrus logger.
func (l *logrusr) copyLogger() *logrusr {
newLogger := &logrusr{
name: make([]string, len(l.name)),
depth: l.depth,
reportCaller: l.reportCaller,
logger: l.logger.Dup(),
defaultFormatter: l.defaultFormatter,
name: make([]string, len(l.name)),
depth: l.depth,
reportCaller: l.reportCaller,
logger: l.logger.Dup(),
formatter: l.formatter,
}

copy(newLogger.name, l.name)
Expand Down

0 comments on commit d198d3e

Please sign in to comment.