Skip to content

✨ Add helpers to configure logger options via pflags #767

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 13 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/onsi/gomega v1.8.1
github.com/prometheus/client_golang v1.0.0
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90
go.uber.org/atomic v1.4.0 // indirect
github.com/spf13/pflag v1.0.5
go.uber.org/zap v1.10.0
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,6 @@ go.mongodb.org/mongo-driver v1.1.2/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qL
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
Expand Down
140 changes: 140 additions & 0 deletions pkg/log/zap/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package zap contains helpers for setting up a new logr.Logger instance
// using the Zap logging framework.
package zap

import (
"fmt"
"strconv"
"strings"

"github.com/spf13/pflag"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var levelStrings = map[string]zapcore.Level{
"debug": zap.DebugLevel,
"-1": zap.DebugLevel,
"info": zap.InfoLevel,
"0": zap.InfoLevel,
"error": zap.ErrorLevel,
"2": zap.ErrorLevel,
"dpanic": zap.DPanicLevel,
"panic": zap.PanicLevel,
"warn": zap.WarnLevel,
"fatal": zap.FatalLevel,
}

type encoderFlag struct {
setFunc func(zapcore.Encoder)
value string
}

var _ pflag.Value = &encoderFlag{}

func (ev *encoderFlag) String() string {
return ev.value
}

func (ev *encoderFlag) Type() string {
return "encoder"
}

func (ev *encoderFlag) Set(flagValue string) error {
val := strings.ToLower(flagValue)
switch val {
case "json":
ev.setFunc(newJSONEncoder())
case "console":
ev.setFunc(newConsoleEncoder())
default:
return fmt.Errorf("invalid encoder value \"%s\"", flagValue)
}
ev.value = flagValue
return nil
}

func newJSONEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
return zapcore.NewJSONEncoder(encoderConfig)
}

func newConsoleEncoder() zapcore.Encoder {
encoderConfig := zap.NewDevelopmentEncoderConfig()
return zapcore.NewConsoleEncoder(encoderConfig)
}

type levelFlag struct {
setFunc func(zap.AtomicLevel)
value string
}

var _ pflag.Value = &levelFlag{}

func (ev *levelFlag) Set(flagValue string) error {
level, validLevel := levelStrings[strings.ToLower(flagValue)]
if !validLevel {
logLevel, err := strconv.Atoi(flagValue)
if err != nil {
return fmt.Errorf("invalid log level \"%s\"", flagValue)
}
if logLevel > 0 {
intLevel := -1 * logLevel
ev.setFunc(zap.NewAtomicLevelAt(zapcore.Level(int8(intLevel))))
} else {
return fmt.Errorf("invalid log level \"%s\"", flagValue)
}
}
ev.setFunc(zap.NewAtomicLevelAt(level))
ev.value = flagValue
return nil
}

func (ev *levelFlag) String() string {
return ev.value
}

func (ev *levelFlag) Type() string {
return "level"
}

type stackTraceFlag struct {
setFunc func(zap.AtomicLevel)
value string
}

var _ pflag.Value = &stackTraceFlag{}

func (ev *stackTraceFlag) Set(flagValue string) error {
level, validLevel := levelStrings[strings.ToLower(flagValue)]
if !validLevel {
return fmt.Errorf("invalid stacktrace level \"%s\"", flagValue)
}
ev.setFunc(zap.NewAtomicLevelAt(level))
ev.value = flagValue
return nil
}

func (ev *stackTraceFlag) String() string {
return ev.value
}

func (ev *stackTraceFlag) Type() string {
return "level"
}
51 changes: 51 additions & 0 deletions pkg/log/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
package zap

import (
"flag"
"io"
"os"
"time"
Expand Down Expand Up @@ -207,3 +208,53 @@ func NewRaw(opts ...Opts) *zap.Logger {
log = log.WithOptions(o.ZapOpts...)
return log
}

// BindFlags will parse the given flagset for zap option flags and set the log options accordingly
// zap-devel: Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn)
// Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)
// zap-encoder: Zap log encoding ('json' or 'console')
// zap-log-level: Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error',
// or any integer value > 0 which corresponds to custom debug levels of increasing verbosity")
// zap-stacktrace-level: Zap Level at and above which stacktraces are captured (one of 'warn' or 'error')
func (o *Options) BindFlags(fs *flag.FlagSet) {

// Set Development mode value
fs.BoolVar(&o.Development, "zap-devel", false,
"Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn). "+
"Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)")

// Set Encoder value
var encVal encoderFlag
encVal.setFunc = func(fromFlag zapcore.Encoder) {
o.Encoder = fromFlag
}
fs.Var(&encVal, "zap-encoder", "Zap log encoding ('json' or 'console')")

// Set the Log Level
var levelVal levelFlag
levelVal.setFunc = func(fromFlag zap.AtomicLevel) {
o.Level = &fromFlag
}
fs.Var(&levelVal, "zap-log-level",
"Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error', "+
"or any integer value > 0 which corresponds to custom debug levels of increasing verbosity")

// Set the StrackTrace Level
var stackVal stackTraceFlag
stackVal.setFunc = func(fromFlag zap.AtomicLevel) {
o.StacktraceLevel = &fromFlag
}
fs.Var(&stackVal, "zap-stacktrace-level",
"Zap Level at and above which stacktraces are captured (one of 'warn' or 'error')")
}

// UseFlagOptions configures the logger to use the Options set by parsing zap option flags from the CLI.
// opts := zap.Options{}
// opts.BindFlags(flag.CommandLine)
// log := zap.New(zap.UseFlagOptions(&opts))
func UseFlagOptions(in *Options) Opts {
return func(o *Options) {
*o = *in
o.addDefaults()
}
}