Skip to content

Commit

Permalink
Merge pull request #320 from presslabs/raw-zap-logger
Browse files Browse the repository at this point in the history
✨ Add zap.RawLoggerTo which returns a configured zap logger
  • Loading branch information
k8s-ci-robot authored Feb 14, 2019
2 parents c2f9dae + 82c389a commit c84fba8
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/log/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ func Logger(development bool) logr.Logger {
// to the given destination, instead of stderr. It otherwise behaves like
// ZapLogger.
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
func RawLoggerTo(destWriter io.Writer, development bool, opts ...zap.Option) *zap.Logger {
// this basically mimics New<type>Config, but with a custom sink
sink := zapcore.AddSync(destWriter)

var enc zapcore.Encoder
var lvl zap.AtomicLevel
var opts []zap.Option
if development {
encCfg := zap.NewDevelopmentEncoderConfig()
enc = zapcore.NewConsoleEncoder(encCfg)
Expand All @@ -64,5 +69,5 @@ func LoggerTo(destWriter io.Writer, development bool) logr.Logger {
opts = append(opts, zap.AddCallerSkip(1), zap.ErrorOutput(sink))
log := zap.New(zapcore.NewCore(&KubeAwareEncoder{Encoder: enc, Verbose: development}, sink, lvl))
log = log.WithOptions(opts...)
return zapr.NewLogger(log)
return log
}

0 comments on commit c84fba8

Please sign in to comment.