Skip to content

Commit 385befd

Browse files
committed
✨ Add predefined functional options for log/zap
1 parent 801e12a commit 385befd

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

pkg/log/zap/zap.go

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ func New(opts ...Opts) logr.Logger {
4343
//
4444
// Deprecated, use New() and the functional opts pattern instead:
4545
//
46-
// New(func(o *Options){
47-
// o.Development: development,
48-
// })
46+
// New(Development(true))
4947
func Logger(development bool) logr.Logger {
5048
return LoggerTo(os.Stderr, development)
5149
}
@@ -56,10 +54,7 @@ func Logger(development bool) logr.Logger {
5654
//
5755
// Deprecated, use New() and the functional opts pattern instead:
5856
//
59-
// New(func(o *Options){
60-
// o.Development: development,
61-
// o.DestWriter: writer,
62-
// })
57+
// New(Development(development), DestWritter(destWriter))
6358
func LoggerTo(destWriter io.Writer, development bool) logr.Logger {
6459
return zapr.NewLogger(RawLoggerTo(destWriter, development))
6560
}
@@ -69,16 +64,9 @@ func LoggerTo(destWriter io.Writer, development bool) logr.Logger {
6964
//
7065
// Deprecated, use NewRaw() and the functional opts pattern instead:
7166
//
72-
// NewRaw(func(o *Options){
73-
// o.Development: development,
74-
// })
67+
// NewRaw(Development(development), DestWritter(destWriter), RawZapOpts(opts))
7568
func RawLoggerTo(destWriter io.Writer, development bool, opts ...zap.Option) *zap.Logger {
76-
o := func(o *Options) {
77-
o.DestWritter = destWriter
78-
o.Development = development
79-
o.ZapOpts = opts
80-
}
81-
return NewRaw(o)
69+
return NewRaw(Development(development), DestWritter(destWriter), RawZapOpts(opts))
8270
}
8371

8472
// Opts allows to manipulate Options
@@ -149,6 +137,48 @@ func (o *Options) addDefaults() {
149137
o.ZapOpts = append(o.ZapOpts, zap.AddStacktrace(o.StacktraceLevel))
150138
}
151139

140+
// Development sets the Development option for the logger
141+
func Development(development bool) func(o *Options) {
142+
return func(o *Options) {
143+
o.Development = development
144+
}
145+
}
146+
147+
// Encoder sets the Encoder option for the logger
148+
func Encoder(encoder zapcore.Encoder) func(o *Options) {
149+
return func(o *Options) {
150+
o.Encoder = encoder
151+
}
152+
}
153+
154+
// DestWritter sets the DestWritter option for the logger
155+
func DestWritter(destWriter io.Writer) func(o *Options) {
156+
return func(o *Options) {
157+
o.DestWritter = destWriter
158+
}
159+
}
160+
161+
// Level sets the Level option for the logger
162+
func Level(level *zap.AtomicLevel) func(o *Options) {
163+
return func(o *Options) {
164+
o.Level = level
165+
}
166+
}
167+
168+
// StacktraceLevel sets the StacktraceLevel option for the logger
169+
func StacktraceLevel(stacktraceLevel *zap.AtomicLevel) func(o *Options) {
170+
return func(o *Options) {
171+
o.StacktraceLevel = stacktraceLevel
172+
}
173+
}
174+
175+
// RawZapOpts appends zapOpts to the raw zap.Options option of the logger
176+
func RawZapOpts(zapOpts []zap.Option) func(o *Options) {
177+
return func(o *Options) {
178+
o.ZapOpts = append(o.ZapOpts, zapOpts...)
179+
}
180+
}
181+
152182
// NewRaw returns a new zap.Logger configured with the passed Opts
153183
// or their defaults. It uses KubeAwareEncoder which adds Type
154184
// information and Namespace/Name to the log.

0 commit comments

Comments
 (0)