-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #865 from DirectXMan12/release/v0.5.2
🏃 Release v0.5.2
- Loading branch information
Showing
10 changed files
with
232 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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