Skip to content

Commit

Permalink
Add logr.Logger.WithCallDepth() support
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Dec 15, 2020
1 parent d6464ae commit 5c54c20
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"flag"

"github.com/go-logr/glogr"
"github.com/go-logr/logr"
"github.com/golang/glog"
)

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) {
log.WithCallDepth(2).Info(msg)
}

func main() {
flag.Set("v", "1")
flag.Set("logtostderr", "true")
Expand All @@ -41,5 +50,6 @@ 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")
glog.Flush()
}
6 changes: 6 additions & 0 deletions glogr.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,10 @@ func (l glogger) WithValues(kvList ...interface{}) logr.Logger {
return new
}

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

var _ logr.Logger = glogger{}

0 comments on commit 5c54c20

Please sign in to comment.