Skip to content

✨ Add helpers for remaining Zap options #639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions pkg/log/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,9 @@ func LoggerTo(destWriter io.Writer, development bool) logr.Logger {
//
// Deprecated: use NewRaw() and the functional opts pattern instead:
//
// NewRaw(UseDevMode(development))
// NewRaw(UseDevMode(development), WriteTo(destWriter), RawZapOpts(opts...))
func RawLoggerTo(destWriter io.Writer, development bool, opts ...zap.Option) *zap.Logger {
o := func(o *Options) {
o.DestWritter = destWriter
o.Development = development
o.ZapOpts = opts
}
return NewRaw(o)
return NewRaw(UseDevMode(development), WriteTo(destWriter), RawZapOpts(opts...))
}

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

// WriteTo configures the logger to write to the given io.Writer, instead of standard error.
// See Options.WriterTo.
// See Options.DestWritter
func WriteTo(out io.Writer) Opts {
return func(o *Options) {
o.DestWritter = out
}
}

// Encoder configures how the logger will encode the output e.g JSON or console.
// See Options.Encoder
func Encoder(encoder zapcore.Encoder) func(o *Options) {
return func(o *Options) {
o.Encoder = encoder
}
}

// Level sets the the minimum enabled logging level e.g Debug, Info
// See Options.Level
func Level(level *zap.AtomicLevel) func(o *Options) {
return func(o *Options) {
o.Level = level
}
}

// StacktraceLevel configures the logger to record a stack trace for all messages at
// or above a given level.
// See Options.StacktraceLevel
func StacktraceLevel(stacktraceLevel *zap.AtomicLevel) func(o *Options) {
return func(o *Options) {
o.StacktraceLevel = stacktraceLevel
}
}

// RawZapOpts allows appending arbitrary zap.Options to configure the underlying zap logger.
// See Options.ZapOpts
func RawZapOpts(zapOpts ...zap.Option) func(o *Options) {
return func(o *Options) {
o.ZapOpts = append(o.ZapOpts, zapOpts...)
}
}

// Options contains all possible settings
type Options struct {
// Development configures the logger to use a Zap development config
Expand Down