Skip to content

Commit

Permalink
Merge pull request #21 from thockin/with-call-depth
Browse files Browse the repository at this point in the history
Add logr.Logger.WithCallDepth() support
  • Loading branch information
thockin authored Jan 23, 2021
2 parents 146009e + aced49e commit 11b4afb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
10 changes: 10 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
)
Expand All @@ -29,6 +30,14 @@ func (e E) Error() string {
return e.str
}

func Helper(log logr.Logger, msg string) {
helper2(log, msg)
}

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

func main() {
log := zapr.NewLogger(zap.NewExample())
log = log.WithName("MyName").WithValues("user", "you")
Expand All @@ -37,4 +46,5 @@ func main() {
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)
Helper(log, "thru a helper")
}
10 changes: 0 additions & 10 deletions go.mod

This file was deleted.

7 changes: 7 additions & 0 deletions zapr.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func (zl *zapLogger) WithName(name string) logr.Logger {
return newLoggerWithExtraSkip(newLogger, 0)
}

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

// Underlier exposes access to the underlying logging implementation. Since
// callers only have a logr.Logger, they have to know which implementation is
// in use, so this interface is less of an abstraction and more of way to test
Expand All @@ -177,3 +181,6 @@ func NewLogger(l *zap.Logger) logr.Logger {
// creates a new logger skipping one level of callstack
return newLoggerWithExtraSkip(l, 1)
}

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

0 comments on commit 11b4afb

Please sign in to comment.