Skip to content

✨ Add helpers for common Zap options #646

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
52 changes: 32 additions & 20 deletions pkg/log/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ func New(opts ...Opts) logr.Logger {
// (stacktraces on warnings, no sampling), otherwise a Zap production
// config will be used (stacktraces on errors, sampling).
//
// Deprecated, use New() and the functional opts pattern instead:
// Deprecated: use New() and the functional opts pattern instead:
//
// New(func(o *Options){
// o.Development = development
// })
// New(UseDevMode(development))
func Logger(development bool) logr.Logger {
return LoggerTo(os.Stderr, development)
}
Expand All @@ -54,24 +52,19 @@ func Logger(development bool) logr.Logger {
// to the given destination, instead of stderr. It otherwise behaves like
// ZapLogger.
//
// Deprecated, use New() and the functional opts pattern instead:
// Deprecated: use New() and the functional opts pattern instead:
//
// New(func(o *Options){
// o.Development = development
// o.DestWriter = writer
// })
// New(UseDevMode(development), WriteTo(writer))
func LoggerTo(destWriter io.Writer, development bool) logr.Logger {
return zapr.NewLogger(RawLoggerTo(destWriter, development))
}

// RawLoggerTo returns a new zap.Logger configured with KubeAwareEncoder
// which logs to a given destination
//
// Deprecated, use NewRaw() and the functional opts pattern instead:
// Deprecated: use NewRaw() and the functional opts pattern instead:
//
// NewRaw(func(o *Options){
// o.Development = development
// })
// NewRaw(UseDevMode(development))
func RawLoggerTo(destWriter io.Writer, development bool, opts ...zap.Option) *zap.Logger {
o := func(o *Options) {
o.DestWritter = destWriter
Expand All @@ -84,25 +77,44 @@ func RawLoggerTo(destWriter io.Writer, development bool, opts ...zap.Option) *za
// Opts allows to manipulate Options
type Opts func(*Options)

// UseDevMode sets the logger to use (or not use) development mode (more
// human-readable output, extra stack traces and logging information, etc).
// See Options.Development
func UseDevMode(enabled bool) Opts {
return func(o *Options) {
o.Development = true
}
}

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

// Options contains all possible settings
type Options struct {
// If Development is true, a Zap development config will be used
// Development configures the logger to use a Zap development config
// (stacktraces on warnings, no sampling), otherwise a Zap production
// config will be used (stacktraces on errors, sampling).
Development bool
// The encoder to use, defaults to console when Development is true
// and JSON otherwise
// Encoder configures how Zap will encode the output. Defaults to
// console when Development is true and JSON otherwise
Encoder zapcore.Encoder
// The destination to write to, defaults to os.Stderr
// DestWritter controls the destination of the log output. Defaults to
// os.Stderr.
DestWritter io.Writer
// The level to use, defaults to Debug when Development is true and
// Info otherwise
// Level configures the verbosity of the logging. Defaults to Debug when
// Development is true and Info otherwise
Level *zap.AtomicLevel
// StacktraceLevel is the level at and above which stacktraces will
// be recorded for all messages. Defaults to Warn when Development
// is true and Error otherwise
StacktraceLevel *zap.AtomicLevel
// Raw zap.Options to configure on the underlying zap logger
// ZapOpts allows passing arbitrary zap.Options to configure on the
// underlying Zap logger.
ZapOpts []zap.Option
}

Expand Down