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 e6c8687 + 3255373 commit 3da5426
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
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) {
logr.WithCallDepth(log, 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()
}
7 changes: 7 additions & 0 deletions glogr.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,11 @@ 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{}
var _ logr.CallDepthLogger = glogger{}

0 comments on commit 3da5426

Please sign in to comment.