Skip to content

Add logr.Logger.WithCallDepth() support #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{}