Skip to content

Commit 8bf540f

Browse files
committed
Add helpers for common Zap options
This adds helpers for common Zap options, so that users don't have to write them themselves. It also fixes the Deprecation warnings to use the standard syntax, and fixes the options godocs to follow proper godocs format.
1 parent 2df793d commit 8bf540f

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

pkg/log/zap/zap.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ func New(opts ...Opts) logr.Logger {
4141
// (stacktraces on warnings, no sampling), otherwise a Zap production
4242
// config will be used (stacktraces on errors, sampling).
4343
//
44-
// Deprecated, use New() and the functional opts pattern instead:
44+
// Deprecated: use New() and the functional opts pattern instead:
4545
//
46-
// New(func(o *Options){
47-
// o.Development = development
48-
// })
46+
// New(UseDevMode(development))
4947
func Logger(development bool) logr.Logger {
5048
return LoggerTo(os.Stderr, development)
5149
}
@@ -54,9 +52,9 @@ func Logger(development bool) logr.Logger {
5452
// to the given destination, instead of stderr. It otherwise behaves like
5553
// ZapLogger.
5654
//
57-
// Deprecated, use New() and the functional opts pattern instead:
55+
// Deprecated: use New() and the functional opts pattern instead:
5856
//
59-
// New(func(o *Options){
57+
// New(UseDevMode(development), WriteTo(writer))
6058
// o.Development = development
6159
// o.DestWriter = writer
6260
// })
@@ -67,11 +65,9 @@ func LoggerTo(destWriter io.Writer, development bool) logr.Logger {
6765
// RawLoggerTo returns a new zap.Logger configured with KubeAwareEncoder
6866
// which logs to a given destination
6967
//
70-
// Deprecated, use NewRaw() and the functional opts pattern instead:
68+
// Deprecated: use NewRaw() and the functional opts pattern instead:
7169
//
72-
// NewRaw(func(o *Options){
73-
// o.Development = development
74-
// })
70+
// NewRaw(UseDevMode(development))
7571
func RawLoggerTo(destWriter io.Writer, development bool, opts ...zap.Option) *zap.Logger {
7672
o := func(o *Options) {
7773
o.DestWritter = destWriter
@@ -84,25 +80,44 @@ func RawLoggerTo(destWriter io.Writer, development bool, opts ...zap.Option) *za
8480
// Opts allows to manipulate Options
8581
type Opts func(*Options)
8682

83+
// UseDevMode sets the logger to use (or not use) development mode (more
84+
// human-readable output, extra stack traces and logging information, etc).
85+
// See Options.Development
86+
func UseDevMode(enabled bool) Opts {
87+
return func(o *Options) {
88+
o.Development = true
89+
}
90+
}
91+
92+
// WriteTo configures the logger to write to the given io.Writer, instead of standard error.
93+
// See Options.WriterTo.
94+
func WriteTo(out io.Writer) Opts {
95+
return func(o *Options) {
96+
o.DestWritter = out
97+
}
98+
}
99+
87100
// Options contains all possible settings
88101
type Options struct {
89-
// If Development is true, a Zap development config will be used
102+
// Development configures the logger to use a Zap development config
90103
// (stacktraces on warnings, no sampling), otherwise a Zap production
91104
// config will be used (stacktraces on errors, sampling).
92105
Development bool
93-
// The encoder to use, defaults to console when Development is true
94-
// and JSON otherwise
106+
// Encoder configures how Zap will encode the output. Defaults to
107+
// console when Development is true and JSON otherwise
95108
Encoder zapcore.Encoder
96-
// The destination to write to, defaults to os.Stderr
109+
// DestWritter controls the destination of the log output. Defaults to
110+
// os.Stderr.
97111
DestWritter io.Writer
98-
// The level to use, defaults to Debug when Development is true and
99-
// Info otherwise
112+
// Level configures the verbosity of the logging. Defaults to Debug when
113+
// Development is true and Info otherwise
100114
Level *zap.AtomicLevel
101115
// StacktraceLevel is the level at and above which stacktraces will
102116
// be recorded for all messages. Defaults to Warn when Development
103117
// is true and Error otherwise
104118
StacktraceLevel *zap.AtomicLevel
105-
// Raw zap.Options to configure on the underlying zap logger
119+
// ZapOpts allows passing arbitrary zap.Options to configure on the
120+
// underlying Zap logger.
106121
ZapOpts []zap.Option
107122
}
108123

0 commit comments

Comments
 (0)