Skip to content

Commit

Permalink
Merge pull request #15 from thockin/master
Browse files Browse the repository at this point in the history
Get rid of InfoLogger, implement V() as additive
  • Loading branch information
thockin committed Jun 11, 2020
2 parents cc6bbcc + 7bc31c2 commit 49ca6b4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 51 deletions.
8 changes: 5 additions & 3 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ limitations under the License.

package main

import "github.com/go-logr/zapr"
import "go.uber.org/zap"
import (
"github.com/go-logr/zapr"
"go.uber.org/zap"
)

type E struct {
str string
Expand All @@ -32,7 +34,7 @@ func main() {
log = log.WithName("MyName").WithValues("user", "you")
log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1})
log.V(1).Info("you should see this")
log.V(3).Info("you should NOT see this")
log.V(1).V(1).Info("you should NOT see this")
log.Error(nil, "uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14})
log.Error(E{"an error occurred"}, "goodbye", "code", -1)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/go-logr/zapr
go 1.12

require (
github.com/go-logr/logr v0.1.0
github.com/go-logr/logr v0.2.0
go.uber.org/atomic v1.3.2
go.uber.org/multierr v1.1.0
go.uber.org/zap v1.8.0
Expand Down
75 changes: 28 additions & 47 deletions zapr.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ limitations under the License.
// For the most part, concepts in Zap correspond directly with those in
// logr.
//
// Unlike Zap, all fields *must* be in the form of suggared fields --
// Unlike Zap, all fields *must* be in the form of sugared fields --
// it's illegal to pass a strongly-typed Zap field in a key position
// to any of the log methods.
//
Expand All @@ -59,40 +59,18 @@ import (
"go.uber.org/zap/zapcore"
)

// noopInfoLogger is a logr.InfoLogger that's always disabled, and does nothing.
type noopInfoLogger struct{}

func (l *noopInfoLogger) Enabled() bool { return false }
func (l *noopInfoLogger) Info(_ string, _ ...interface{}) {}

var disabledInfoLogger = &noopInfoLogger{}

// NB: right now, we always use the equivalent of sugared logging.
// This is necessary, since logr doesn't define non-suggared types,
// and using zap-specific non-suggared types would make uses tied
// directly to Zap.

// infoLogger is a logr.InfoLogger that uses Zap to log at a particular
// level. The level has already been converted to a Zap level, which
// is to say that `logrLevel = -1*zapLevel`.
type infoLogger struct {
lvl zapcore.Level
l *zap.Logger
}

func (l *infoLogger) Enabled() bool { return true }
func (l *infoLogger) Info(msg string, keysAndVals ...interface{}) {
if checkedEntry := l.l.Check(l.lvl, msg); checkedEntry != nil {
checkedEntry.Write(handleFields(l.l, keysAndVals)...)
}
}

// zapLogger is a logr.Logger that uses Zap to log.
// zapLogger is a logr.Logger that uses Zap to log. The level has already been
// converted to a Zap level, which is to say that `logrLevel = -1*zapLevel`.
type zapLogger struct {
// NB: this looks very similar to zap.SugaredLogger, but
// deals with our desire to have multiple verbosity levels.
l *zap.Logger
infoLogger
l *zap.Logger
lvl zapcore.Level
}

// handleFields converts a bunch of arbitrary key-value pairs into Zap fields. It takes
Expand Down Expand Up @@ -140,42 +118,45 @@ func handleFields(l *zap.Logger, args []interface{}, additional ...zap.Field) []
return append(fields, additional...)
}

func (l *zapLogger) Error(err error, msg string, keysAndVals ...interface{}) {
if checkedEntry := l.l.Check(zap.ErrorLevel, msg); checkedEntry != nil {
checkedEntry.Write(handleFields(l.l, keysAndVals, zap.Error(err))...)
func (zl *zapLogger) Enabled() bool {
return zl.l.Core().Enabled(zl.lvl)
}

func (zl *zapLogger) Info(msg string, keysAndVals ...interface{}) {
if checkedEntry := zl.l.Check(zl.lvl, msg); checkedEntry != nil {
checkedEntry.Write(handleFields(zl.l, keysAndVals)...)
}
}

func (l *zapLogger) V(level int) logr.InfoLogger {
lvl := zapcore.Level(-1 * level)
if l.l.Core().Enabled(lvl) {
return &infoLogger{
lvl: lvl,
l: l.l,
}
func (zl *zapLogger) Error(err error, msg string, keysAndVals ...interface{}) {
if checkedEntry := zl.l.Check(zap.ErrorLevel, msg); checkedEntry != nil {
checkedEntry.Write(handleFields(zl.l, keysAndVals, zap.Error(err))...)
}
}

func (zl *zapLogger) V(level int) logr.Logger {
return &zapLogger{
lvl: zl.lvl - zapcore.Level(level),
l: zl.l,
}
return disabledInfoLogger
}

func (l *zapLogger) WithValues(keysAndValues ...interface{}) logr.Logger {
newLogger := l.l.With(handleFields(l.l, keysAndValues)...)
func (zl *zapLogger) WithValues(keysAndValues ...interface{}) logr.Logger {
newLogger := zl.l.With(handleFields(zl.l, keysAndValues)...)
return newLoggerWithExtraSkip(newLogger, 0)
}

func (l *zapLogger) WithName(name string) logr.Logger {
newLogger := l.l.Named(name)
func (zl *zapLogger) WithName(name string) logr.Logger {
newLogger := zl.l.Named(name)
return newLoggerWithExtraSkip(newLogger, 0)
}

// newLoggerWithExtraSkip allows creation of loggers with variable levels of callstack skipping
func newLoggerWithExtraSkip(l *zap.Logger, callerSkip int) logr.Logger {
log := l.WithOptions(zap.AddCallerSkip(callerSkip))
return &zapLogger{
l: log,
infoLogger: infoLogger{
l: log,
lvl: zap.InfoLevel,
},
l: log,
lvl: zap.InfoLevel,
}
}

Expand Down

0 comments on commit 49ca6b4

Please sign in to comment.