Skip to content

Commit 9940425

Browse files
Fix log depth for DelegatingLogSink
1 parent bcde6f0 commit 9940425

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

examples/scratch-env/main.go

Lines changed: 6 additions & 3 deletions
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

Lines changed: 3 additions & 0 deletions
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

pkg/log/zap/zap.go

Lines changed: 1 addition & 1 deletion
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)