Skip to content

Commit 797c6b5

Browse files
Fix log depth for DelegatingLogSink
1 parent f27ed4a commit 797c6b5

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

examples/scratch-env/main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import (
2121
"os"
2222

2323
flag "github.com/spf13/pflag"
24+
"go.uber.org/zap"
2425

2526
ctrl "sigs.k8s.io/controller-runtime"
2627
"sigs.k8s.io/controller-runtime/pkg/envtest"
27-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
28+
logzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
2829
)
2930

3031
var (
@@ -35,16 +36,18 @@ var (
3536

3637
// have a separate function so we can return an exit code w/o skipping defers
3738
func runMain() int {
38-
loggerOpts := &zap.Options{
39+
loggerOpts := &logzap.Options{
3940
Development: true, // a sane default
41+
ZapOpts: []zap.Option{zap.AddCaller()},
4042
}
4143
{
4244
var goFlagSet goflag.FlagSet
4345
loggerOpts.BindFlags(&goFlagSet)
4446
flag.CommandLine.AddGoFlagSet(&goFlagSet)
4547
}
4648
flag.Parse()
47-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(loggerOpts)))
49+
ctrl.SetLogger(logzap.New(logzap.UseFlagOptions(loggerOpts)))
50+
ctrl.Log.Info("Starting...")
4851

4952
log := ctrl.Log.WithName("main")
5053

pkg/log/deleg.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ func (p *loggerPromise) Fulfill(parentLogSink logr.LogSink) {
7373

7474
p.logger.lock.Lock()
7575
p.logger.logger = sink
76+
if withCallDepth, ok := sink.(logr.CallDepthLogSink); ok {
77+
p.logger.logger = withCallDepth.WithCallDepth(1)
78+
}
7679
p.logger.promise = nil
7780
p.logger.lock.Unlock()
7881

@@ -141,7 +144,11 @@ func (l *DelegatingLogSink) WithName(name string) logr.LogSink {
141144
defer l.lock.RUnlock()
142145

143146
if l.promise == nil {
144-
return l.logger.WithName(name)
147+
sink := l.logger.WithName(name)
148+
if withCallDepth, ok := sink.(logr.CallDepthLogSink); ok {
149+
sink = withCallDepth.WithCallDepth(-1)
150+
}
151+
return sink
145152
}
146153

147154
res := &DelegatingLogSink{logger: l.logger}
@@ -157,7 +164,11 @@ func (l *DelegatingLogSink) WithValues(tags ...interface{}) logr.LogSink {
157164
defer l.lock.RUnlock()
158165

159166
if l.promise == nil {
160-
return l.logger.WithValues(tags...)
167+
sink := l.logger.WithValues(tags...)
168+
if withCallDepth, ok := sink.(logr.CallDepthLogSink); ok {
169+
sink = withCallDepth.WithCallDepth(-1)
170+
}
171+
return sink
161172
}
162173

163174
res := &DelegatingLogSink{logger: l.logger}

pkg/log/zap/zap.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func NewRaw(opts ...Opts) *zap.Logger {
244244
// this basically mimics New<type>Config, but with a custom sink
245245
sink := zapcore.AddSync(o.DestWriter)
246246

247-
o.ZapOpts = append(o.ZapOpts, zap.AddCallerSkip(1), zap.ErrorOutput(sink))
247+
o.ZapOpts = append(o.ZapOpts, zap.ErrorOutput(sink))
248248
log := zap.New(zapcore.NewCore(&KubeAwareEncoder{Encoder: o.Encoder, Verbose: o.Development}, sink, o.Level))
249249
log = log.WithOptions(o.ZapOpts...)
250250
return log

0 commit comments

Comments
 (0)