Skip to content

Commit 3fba417

Browse files
committed
✨ Add helpers for remaining Zap options
1 parent 81842d0 commit 3fba417

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

pkg/log/zap/zap.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,9 @@ func LoggerTo(destWriter io.Writer, development bool) logr.Logger {
6464
//
6565
// Deprecated: use NewRaw() and the functional opts pattern instead:
6666
//
67-
// NewRaw(UseDevMode(development))
67+
// NewRaw(UseDevMode(development), WriteTo(destWriter), RawZapOpts(opts...))
6868
func RawLoggerTo(destWriter io.Writer, development bool, opts ...zap.Option) *zap.Logger {
69-
o := func(o *Options) {
70-
o.DestWritter = destWriter
71-
o.Development = development
72-
o.ZapOpts = opts
73-
}
74-
return NewRaw(o)
69+
return NewRaw(UseDevMode(development), WriteTo(destWriter), RawZapOpts(opts...))
7570
}
7671

7772
// Opts allows to manipulate Options
@@ -87,13 +82,46 @@ func UseDevMode(enabled bool) Opts {
8782
}
8883

8984
// WriteTo configures the logger to write to the given io.Writer, instead of standard error.
90-
// See Options.WriterTo.
85+
// See Options.DestWritter
9186
func WriteTo(out io.Writer) Opts {
9287
return func(o *Options) {
9388
o.DestWritter = out
9489
}
9590
}
9691

92+
// Encoder configures how the logger will encode the output e.g JSON or console.
93+
// See Options.Encoder
94+
func Encoder(encoder zapcore.Encoder) func(o *Options) {
95+
return func(o *Options) {
96+
o.Encoder = encoder
97+
}
98+
}
99+
100+
// Level sets the the minimum enabled logging level e.g Debug, Info
101+
// See Options.Level
102+
func Level(level *zap.AtomicLevel) func(o *Options) {
103+
return func(o *Options) {
104+
o.Level = level
105+
}
106+
}
107+
108+
// StacktraceLevel configures the logger to record a stack trace for all messages at
109+
// or above a given level.
110+
// See Options.StacktraceLevel
111+
func StacktraceLevel(stacktraceLevel *zap.AtomicLevel) func(o *Options) {
112+
return func(o *Options) {
113+
o.StacktraceLevel = stacktraceLevel
114+
}
115+
}
116+
117+
// RawZapOpts allows appending arbitrary zap.Options to configure the underlying zap logger.
118+
// See Options.ZapOpts
119+
func RawZapOpts(zapOpts ...zap.Option) func(o *Options) {
120+
return func(o *Options) {
121+
o.ZapOpts = append(o.ZapOpts, zapOpts...)
122+
}
123+
}
124+
97125
// Options contains all possible settings
98126
type Options struct {
99127
// Development configures the logger to use a Zap development config

0 commit comments

Comments
 (0)