Skip to content

Commit ecdbe54

Browse files
authored
Merge pull request #646 from DirectXMan12/feature/zap-option-helpers
✨ Add helpers for common Zap options
2 parents 2df793d + 6d830ff commit ecdbe54

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

pkg/log/zap/zap.go

Lines changed: 32 additions & 20 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,24 +52,19 @@ 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){
60-
// o.Development = development
61-
// o.DestWriter = writer
62-
// })
57+
// New(UseDevMode(development), WriteTo(writer))
6358
func LoggerTo(destWriter io.Writer, development bool) logr.Logger {
6459
return zapr.NewLogger(RawLoggerTo(destWriter, development))
6560
}
6661

6762
// RawLoggerTo returns a new zap.Logger configured with KubeAwareEncoder
6863
// which logs to a given destination
6964
//
70-
// Deprecated, use NewRaw() and the functional opts pattern instead:
65+
// Deprecated: use NewRaw() and the functional opts pattern instead:
7166
//
72-
// NewRaw(func(o *Options){
73-
// o.Development = development
74-
// })
67+
// NewRaw(UseDevMode(development))
7568
func RawLoggerTo(destWriter io.Writer, development bool, opts ...zap.Option) *zap.Logger {
7669
o := func(o *Options) {
7770
o.DestWritter = destWriter
@@ -84,25 +77,44 @@ func RawLoggerTo(destWriter io.Writer, development bool, opts ...zap.Option) *za
8477
// Opts allows to manipulate Options
8578
type Opts func(*Options)
8679

80+
// UseDevMode sets the logger to use (or not use) development mode (more
81+
// human-readable output, extra stack traces and logging information, etc).
82+
// See Options.Development
83+
func UseDevMode(enabled bool) Opts {
84+
return func(o *Options) {
85+
o.Development = true
86+
}
87+
}
88+
89+
// WriteTo configures the logger to write to the given io.Writer, instead of standard error.
90+
// See Options.WriterTo.
91+
func WriteTo(out io.Writer) Opts {
92+
return func(o *Options) {
93+
o.DestWritter = out
94+
}
95+
}
96+
8797
// Options contains all possible settings
8898
type Options struct {
89-
// If Development is true, a Zap development config will be used
99+
// Development configures the logger to use a Zap development config
90100
// (stacktraces on warnings, no sampling), otherwise a Zap production
91101
// config will be used (stacktraces on errors, sampling).
92102
Development bool
93-
// The encoder to use, defaults to console when Development is true
94-
// and JSON otherwise
103+
// Encoder configures how Zap will encode the output. Defaults to
104+
// console when Development is true and JSON otherwise
95105
Encoder zapcore.Encoder
96-
// The destination to write to, defaults to os.Stderr
106+
// DestWritter controls the destination of the log output. Defaults to
107+
// os.Stderr.
97108
DestWritter io.Writer
98-
// The level to use, defaults to Debug when Development is true and
99-
// Info otherwise
109+
// Level configures the verbosity of the logging. Defaults to Debug when
110+
// Development is true and Info otherwise
100111
Level *zap.AtomicLevel
101112
// StacktraceLevel is the level at and above which stacktraces will
102113
// be recorded for all messages. Defaults to Warn when Development
103114
// is true and Error otherwise
104115
StacktraceLevel *zap.AtomicLevel
105-
// Raw zap.Options to configure on the underlying zap logger
116+
// ZapOpts allows passing arbitrary zap.Options to configure on the
117+
// underlying Zap logger.
106118
ZapOpts []zap.Option
107119
}
108120

0 commit comments

Comments
 (0)