Skip to content

Commit 66261bf

Browse files
authored
Merge pull request #7 from thockin/with-call-depth
Add logr.Logger.WithCallDepth() support
2 parents 33b6352 + 98b7ae5 commit 66261bf

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

example/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
stdlog "log"
2121
"os"
2222

23+
"github.com/go-logr/logr"
2324
"github.com/go-logr/stdr"
2425
)
2526

@@ -31,6 +32,14 @@ func (e E) Error() string {
3132
return e.str
3233
}
3334

35+
func Helper(log logr.Logger, msg string) {
36+
helper2(log, msg)
37+
}
38+
39+
func helper2(log logr.Logger, msg string) {
40+
logr.WithCallDepth(log, 2).Info(msg)
41+
}
42+
3443
func main() {
3544
stdr.SetVerbosity(1)
3645
log := stdr.NewWithOptions(stdlog.New(os.Stderr, "", stdlog.LstdFlags), stdr.Options{LogCaller: stdr.All})
@@ -40,4 +49,5 @@ func main() {
4049
log.V(1).V(1).Info("you should NOT see this")
4150
log.Error(nil, "uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14})
4251
log.Error(E{"an error occurred"}, "goodbye", "code", -1)
52+
Helper(log, "thru a helper")
4353
}

stdr.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,14 @@ func (l logger) WithValues(kvList ...interface{}) logr.Logger {
263263
return new
264264
}
265265

266+
func (l logger) WithCallDepth(depth int) logr.Logger {
267+
new := l.clone()
268+
new.depth += depth
269+
return new
270+
}
271+
266272
var _ logr.Logger = logger{}
273+
var _ logr.CallDepthLogger = logger{}
267274

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

0 commit comments

Comments
 (0)