-
Notifications
You must be signed in to change notification settings - Fork 1.2k
✨ 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
k8s-ci-robot
merged 13 commits into
kubernetes-sigs:master
from
bharathi-tenneti:master
Mar 11, 2020
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9ed6d0a
Flags supported added
bharathi-tenneti c9a71ba
stracktrace flag added - not working
bharathi-tenneti 7943096
Working flags added
bharathi-tenneti 3b28488
Cleaned up commets
bharathi-tenneti 6567aca
Addressing PR coments
bharathi-tenneti 334ea25
Merge branch 'upstream-master'
bharathi-tenneti 435f612
Zap levels added, and custom log levels added
bharathi-tenneti dd4c92b
map added for levelStrings replacing switch, and formatting changes
bharathi-tenneti d17d9c9
Fixing few Error messaages
bharathi-tenneti e7d7f4c
Update pkg/log/zap/zap.go
bharathi-tenneti a1ebd06
Header comments fixed for godoc
bharathi-tenneti 06787b6
Merge branch 'master' of github.com:bharathi-tenneti/controller-runtime
bharathi-tenneti 4ced175
Merge branch 'master' of github.com:kubernetes-sigs/controller-runtime
bharathi-tenneti 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
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,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" | ||
} |
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
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.