Skip to content

Commit

Permalink
Merge pull request #28 from thockin/prep-v1
Browse files Browse the repository at this point in the history
Update zapr to logr v1.0.0-rc1
  • Loading branch information
thockin committed Jul 6, 2021
2 parents 9f3e0b1 + 9b2d2a9 commit 91a4822
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
*~
*.swp
/vendor
8 changes: 6 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ func Helper(log logr.Logger, msg string) {
}

func helper2(log logr.Logger, msg string) {
logr.WithCallDepth(log, 2).Info(msg)
log.WithCallDepth(2).Info(msg)
}

func main() {
log := zapr.NewLogger(zap.NewExample())
zc := zap.NewProductionConfig()
zc.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
zc.DisableStacktrace = true
z, _ := zc.Build()
log := zapr.NewLogger(z)
log = log.WithName("MyName")
example(log.WithValues("module", "example"))
}
Expand Down
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.16

require (
github.com/go-logr/logr v0.4.0
github.com/go-logr/logr v1.0.0-rc1
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
go.uber.org/atomic v1.3.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc=
github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v1.0.0-rc1 h1:+ul9F74rBkPajeP8m4o3o0tiglmzNFsPnuhYyBCQ0Sc=
github.com/go-logr/logr v1.0.0-rc1/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
49 changes: 27 additions & 22 deletions zapr.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ import (
type zapLogger struct {
// NB: this looks very similar to zap.SugaredLogger, but
// deals with our desire to have multiple verbosity levels.
l *zap.Logger
lvl zapcore.Level
l *zap.Logger
}

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

func (zl *zapLogger) Enabled() bool {
return zl.l.Core().Enabled(zl.lvl)
func (zl *zapLogger) Init(ri logr.RuntimeInfo) {
zl.l = zl.l.WithOptions(zap.AddCallerSkip(ri.CallDepth))
}

func (zl *zapLogger) Info(msg string, keysAndVals ...interface{}) {
if checkedEntry := zl.l.Check(zl.lvl, msg); checkedEntry != nil {
// Zap levels are int8 - make sure we stay in bounds. logr itself should
// ensure we never get negative values.
func toZapLevel(lvl int) zapcore.Level {
if lvl > 127 {
lvl = 127
}
// zap levels are inverted.
return 0 - zapcore.Level(lvl)
}

func (zl zapLogger) Enabled(lvl int) bool {
return zl.l.Core().Enabled(toZapLevel(lvl))
}

func (zl *zapLogger) Info(lvl int, msg string, keysAndVals ...interface{}) {
if checkedEntry := zl.l.Check(toZapLevel(lvl), msg); checkedEntry != nil {
checkedEntry.Write(handleFields(zl.l, keysAndVals)...)
}
}
Expand All @@ -134,24 +147,17 @@ func (zl *zapLogger) Error(err error, msg string, keysAndVals ...interface{}) {
}
}

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

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

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

func (zl *zapLogger) WithCallDepth(depth int) logr.Logger {
func (zl *zapLogger) WithCallDepth(depth int) logr.LogSink {
return newLoggerWithExtraSkip(zl.l, depth)
}

Expand All @@ -168,19 +174,18 @@ func (zl *zapLogger) GetUnderlying() *zap.Logger {
}

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

// NewLogger creates a new logr.Logger using the given Zap Logger to log.
func NewLogger(l *zap.Logger) logr.Logger {
// creates a new logger skipping one level of callstack
return newLoggerWithExtraSkip(l, 1)
return logr.New(newLoggerWithExtraSkip(l, 1))
}

var _ logr.Logger = &zapLogger{}
var _ logr.CallDepthLogger = &zapLogger{}
var _ logr.LogSink = &zapLogger{}
var _ logr.CallDepthLogSink = &zapLogger{}

0 comments on commit 91a4822

Please sign in to comment.