-
Notifications
You must be signed in to change notification settings - Fork 1.8k
pkg/log/zap: adding helpers for zap logger configuration #873
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
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d61f68c
pkg/log/zap: adding helpers for zap logger configuration
joelanford eae7198
pkg/scaffold/cmd.go: adding zap logger helper to scaffold
joelanford 03d152c
pkg/log/zap/flags.go: improve zap-devel help text, group sampleValue …
joelanford c283dc1
pkg/log/zap/flags.go: remove unnecessary import newlines
joelanford 641106d
pkg/log/zap: remove factory; s/sink/syncer/g
joelanford bf391e9
test/*,commands/operator-sdk/cmd/up/local.go: include flags registere…
joelanford ccaf53f
pkg/log/zap: bump copyright year to 2019
joelanford 975eddc
pkg/scaffold: include flags registered by imported packages
joelanford d8e6561
Merge branch 'master' into logger
joelanford c90c6fa
pkg/(ansible|helm)/flags,commands,test: centralizing zap flagset setup
joelanford fc945ce
pkg/scaffold/,test/test-framework: whitespace and comment consistency
joelanford dead347
Merge branch 'master' into logger
joelanford 14e4774
Merge branch 'master' into logger
joelanford 3f8bdc2
Merge branch 'master' into logger
joelanford 7b19095
Merge branch 'master' into logger
joelanford e7c77ae
images/scorecard-proxy/cmd/proxy/main.go: use configurable zap logger
joelanford ecd0200
Merge branch 'master' into logger
joelanford 98d0398
pkg/log/zap/flags.go: only allow debug, info, and error
joelanford 319fd2f
Merge branch 'master' into logger
joelanford e6ef2ca
images/scorecard-proxy/cmd/proxy/main.go: fix imports
joelanford 48f07e0
doc/user/logging.md: document zap helpers and flags
joelanford File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Copyright 2019 The Operator-SDK 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 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/pflag" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
| ) | ||
|
|
||
| var ( | ||
| zapFlagSet *pflag.FlagSet | ||
|
|
||
| development bool | ||
| encoderVal encoderValue | ||
| levelVal levelValue | ||
| sampleVal sampleValue | ||
| ) | ||
|
|
||
| func init() { | ||
| zapFlagSet = pflag.NewFlagSet("zap", pflag.ExitOnError) | ||
| zapFlagSet.BoolVar(&development, "zap-devel", false, "Enable zap development mode (changes defaults to console encoder, debug log level, and disables sampling)") | ||
| zapFlagSet.Var(&encoderVal, "zap-encoder", "Zap log encoding ('json' or 'console')") | ||
| zapFlagSet.Var(&levelVal, "zap-level", "Zap log level (one of 'debug', 'info', 'error')") | ||
| zapFlagSet.Var(&sampleVal, "zap-sample", "Enable zap log sampling") | ||
| } | ||
|
|
||
| func FlagSet() *pflag.FlagSet { | ||
| return zapFlagSet | ||
| } | ||
|
|
||
| type encoderValue struct { | ||
| set bool | ||
| encoder zapcore.Encoder | ||
| str string | ||
| } | ||
|
|
||
| func (v *encoderValue) Set(e string) error { | ||
| v.set = true | ||
| switch e { | ||
| case "json": | ||
| v.encoder = jsonEncoder() | ||
| case "console": | ||
| v.encoder = consoleEncoder() | ||
| default: | ||
| return fmt.Errorf("unknown encoder \"%s\"", e) | ||
| } | ||
| v.str = e | ||
| return nil | ||
| } | ||
|
|
||
| func (v encoderValue) String() string { | ||
| return v.str | ||
| } | ||
|
|
||
| func (v encoderValue) Type() string { | ||
| return "encoder" | ||
| } | ||
|
|
||
| func jsonEncoder() zapcore.Encoder { | ||
| encoderConfig := zap.NewProductionEncoderConfig() | ||
| return zapcore.NewJSONEncoder(encoderConfig) | ||
| } | ||
|
|
||
| func consoleEncoder() zapcore.Encoder { | ||
| encoderConfig := zap.NewDevelopmentEncoderConfig() | ||
| return zapcore.NewConsoleEncoder(encoderConfig) | ||
| } | ||
|
|
||
| type levelValue struct { | ||
| set bool | ||
| level zapcore.Level | ||
| } | ||
|
|
||
| func (v *levelValue) Set(l string) error { | ||
| v.set = true | ||
| lower := strings.ToLower(l) | ||
| switch lower { | ||
| case "debug", "info", "error": | ||
| return v.level.Set(l) | ||
| } | ||
| return fmt.Errorf("invalid log level \"%s\"", l) | ||
| } | ||
|
|
||
| func (v levelValue) String() string { | ||
| return v.level.String() | ||
| } | ||
|
|
||
| func (v levelValue) Type() string { | ||
| return "level" | ||
| } | ||
|
|
||
| type sampleValue struct { | ||
| set bool | ||
| sample bool | ||
| } | ||
|
|
||
| func (v *sampleValue) Set(s string) error { | ||
| var err error | ||
| v.set = true | ||
| v.sample, err = strconv.ParseBool(s) | ||
| return err | ||
| } | ||
|
|
||
| func (v sampleValue) String() string { | ||
| return strconv.FormatBool(v.sample) | ||
| } | ||
|
|
||
| func (v sampleValue) IsBoolFlag() bool { | ||
| return true | ||
| } | ||
|
|
||
| func (v sampleValue) Type() string { | ||
| return "sample" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright 2019 The Operator-SDK 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 | ||
|
|
||
| import ( | ||
| "io" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/go-logr/zapr" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
| logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" | ||
| ) | ||
|
|
||
| func Logger() logr.Logger { | ||
| return LoggerTo(os.Stderr) | ||
| } | ||
|
|
||
| func LoggerTo(destWriter io.Writer) logr.Logger { | ||
| syncer := zapcore.AddSync(destWriter) | ||
| conf := getConfig(destWriter) | ||
|
|
||
| conf.encoder = &logf.KubeAwareEncoder{Encoder: conf.encoder, Verbose: conf.level.Level() < 0} | ||
| if conf.sample { | ||
| conf.opts = append(conf.opts, zap.WrapCore(func(core zapcore.Core) zapcore.Core { | ||
| return zapcore.NewSampler(core, time.Second, 100, 100) | ||
| })) | ||
| } | ||
| conf.opts = append(conf.opts, zap.AddCallerSkip(1), zap.ErrorOutput(syncer)) | ||
| log := zap.New(zapcore.NewCore(conf.encoder, syncer, conf.level)) | ||
| log = log.WithOptions(conf.opts...) | ||
| return zapr.NewLogger(log) | ||
| } | ||
|
|
||
| type config struct { | ||
| encoder zapcore.Encoder | ||
| level zap.AtomicLevel | ||
| sample bool | ||
| opts []zap.Option | ||
| } | ||
|
|
||
| func getConfig(destWriter io.Writer) config { | ||
| var c config | ||
|
|
||
| // Set the defaults depending on the log mode (development vs. production) | ||
| if development { | ||
| c.encoder = consoleEncoder() | ||
| c.level = zap.NewAtomicLevelAt(zap.DebugLevel) | ||
| c.opts = append(c.opts, zap.Development(), zap.AddStacktrace(zap.ErrorLevel)) | ||
| c.sample = false | ||
| } else { | ||
| c.encoder = jsonEncoder() | ||
| c.level = zap.NewAtomicLevelAt(zap.InfoLevel) | ||
| c.opts = append(c.opts, zap.AddStacktrace(zap.WarnLevel)) | ||
| c.sample = true | ||
| } | ||
|
|
||
| // Override the defaults if the flags were set explicitly on the command line | ||
| if encoderVal.set { | ||
| c.encoder = encoderVal.encoder | ||
| } | ||
| if levelVal.set { | ||
| c.level = zap.NewAtomicLevelAt(levelVal.level) | ||
| } | ||
| if sampleVal.set { | ||
| c.sample = sampleVal.sample | ||
| } | ||
|
|
||
| return c | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.