Skip to content

Commit e2e258d

Browse files
committed
Use zapcore instead of logr overwrite
The previous implementation drops some functionality and information, while this one works with the underlying zapcore filter.
1 parent 40d77fd commit e2e258d

File tree

3 files changed

+28
-35
lines changed

3 files changed

+28
-35
lines changed

cmd/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
// to ensure that exec-entrypoint and run can make use of them.
2727
_ "k8s.io/client-go/plugin/pkg/client/auth"
2828

29+
"go.uber.org/zap"
2930
"go.uber.org/zap/zapcore"
3031
corev1 "k8s.io/api/core/v1"
3132
"k8s.io/apimachinery/pkg/labels"
@@ -37,7 +38,7 @@ import (
3738
"sigs.k8s.io/controller-runtime/pkg/cache"
3839
"sigs.k8s.io/controller-runtime/pkg/client"
3940
"sigs.k8s.io/controller-runtime/pkg/healthz"
40-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
41+
ctrlzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
4142
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
4243
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
4344
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -92,14 +93,15 @@ func main() {
9293
flag.StringVar(&certificateIssuerName, "certificate-issuer-name", "nova-hypervisor-agents-ca-issuer",
9394
"Name of the certificate issuer.")
9495

95-
opts := zap.Options{
96+
opts := ctrlzap.Options{
9697
Development: true,
9798
TimeEncoder: zapcore.ISO8601TimeEncoder,
99+
ZapOpts: []zap.Option{zap.WrapCore(logger.WrapCore)},
98100
}
99101
opts.BindFlags(flag.CommandLine)
100102
flag.Parse()
101103

102-
ctrl.SetLogger(logger.WrapLogger(zap.New(zap.UseFlagOptions(&opts))))
104+
ctrl.SetLogger(ctrlzap.New(ctrlzap.UseFlagOptions(&opts)))
103105

104106
// if the enable-http2 flag is false (the default), http/2 should be disabled
105107
// due to its vulnerabilities. More specifically, disabling http/2 will

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ require (
4040
github.com/felixge/httpsnoop v1.0.4 // indirect
4141
github.com/fsnotify/fsnotify v1.9.0 // indirect
4242
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
43-
github.com/go-logr/logr v1.4.3
43+
github.com/go-logr/logr v1.4.3 // indirect
4444
github.com/go-logr/stdr v1.2.2 // indirect
4545
github.com/go-logr/zapr v1.3.0 // indirect
4646
github.com/go-openapi/jsonpointer v0.21.0 // indirect

internal/logger/logger.go

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,41 @@ limitations under the License.
1818
package logger
1919

2020
import (
21-
"github.com/go-logr/logr"
21+
"go.uber.org/zap/zapcore"
2222
)
2323

24-
type wrapper struct {
25-
orig logr.LogSink
24+
type wrapCore struct {
25+
core zapcore.Core
2626
}
2727

28-
// Enabled implements logr.LogSink.
29-
func (w wrapper) Enabled(level int) bool {
30-
return w.orig.Enabled(level)
28+
func WrapCore(core zapcore.Core) zapcore.Core {
29+
return wrapCore{core}
3130
}
3231

33-
// Error implements logr.LogSink.
34-
func (w wrapper) Error(err error, msg string, keysAndValues ...any) {
35-
if msg == "Reconciler error" {
36-
w.orig.Info(2, msg, append(keysAndValues, "error", err.Error())...)
37-
} else {
38-
w.orig.Error(err, msg, keysAndValues...)
32+
// Check implements zapcore.Core.
33+
func (w wrapCore) Check(entry zapcore.Entry, checkedEntry *zapcore.CheckedEntry) *zapcore.CheckedEntry {
34+
if entry.Message == "Reconciler error" {
35+
entry.Level = -2
3936
}
37+
return w.core.Check(entry, checkedEntry)
4038
}
4139

42-
// Info implements logr.LogSink.
43-
func (w wrapper) Info(level int, msg string, keysAndValues ...any) {
44-
w.orig.Info(level, msg, keysAndValues...)
40+
// Enabled implements zapcore.Core.
41+
func (w wrapCore) Enabled(level zapcore.Level) bool {
42+
return w.core.Enabled(level)
4543
}
4644

47-
// Init implements logr.LogSink.
48-
func (w wrapper) Init(info logr.RuntimeInfo) {
49-
w.orig.Init(info)
45+
// Sync implements zapcore.Core.
46+
func (w wrapCore) Sync() error {
47+
return w.core.Sync()
5048
}
5149

52-
// WithName implements logr.LogSink.
53-
func (w wrapper) WithName(name string) logr.LogSink {
54-
w.orig.WithName(name)
55-
return w
50+
// With implements zapcore.Core.
51+
func (w wrapCore) With(fields []zapcore.Field) zapcore.Core {
52+
return wrapCore{w.core.With(fields)}
5653
}
5754

58-
// WithValues implements logr.LogSink.
59-
func (w wrapper) WithValues(keysAndValues ...any) logr.LogSink {
60-
w.orig.WithValues(keysAndValues...)
61-
return w
62-
}
63-
64-
func WrapLogger(logger logr.Logger) logr.Logger {
65-
orig := logger.GetSink()
66-
return logger.WithSink(wrapper{orig})
55+
// Write implements zapcore.Core.
56+
func (w wrapCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
57+
return w.core.Write(entry, fields)
6758
}

0 commit comments

Comments
 (0)