Skip to content

Commit

Permalink
Merge pull request #7 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 committed Jan 23, 2021
2 parents 33b6352 + 98b7ae5 commit 66261bf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions example/main.go
Expand Up @@ -20,6 +20,7 @@ import (
stdlog "log"
"os"

"github.com/go-logr/logr"
"github.com/go-logr/stdr"
)

Expand All @@ -31,6 +32,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() {
stdr.SetVerbosity(1)
log := stdr.NewWithOptions(stdlog.New(os.Stderr, "", stdlog.LstdFlags), stdr.Options{LogCaller: stdr.All})
Expand All @@ -40,4 +49,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")
}
7 changes: 7 additions & 0 deletions stdr.go
Expand Up @@ -263,7 +263,14 @@ func (l logger) WithValues(kvList ...interface{}) logr.Logger {
return new
}

func (l logger) WithCallDepth(depth int) logr.Logger {
new := l.clone()
new.depth += depth
return new
}

var _ logr.Logger = logger{}
var _ logr.CallDepthLogger = logger{}

// Underlier exposes access to the underlying logging implementation. Since
// callers only have a logr.Logger, they have to know which implementation is
Expand Down

0 comments on commit 66261bf

Please sign in to comment.