-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Do not log in log library #1364
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,38 +19,49 @@ package logging | |
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/knative/serving/pkg/logging/logkey" | ||
| "github.com/josephburnett/k8sflag/pkg/k8sflag" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
| ) | ||
|
|
||
| // DefaultLoggingConfigPath is the path where the zap logging | ||
| // config will be written in a default Knative deployment. | ||
| const DefaultLoggingConfigPath = "/etc/config-logging" | ||
|
|
||
| // NewLogger creates a logger with the supplied configuration. | ||
| // If configuration is empty, a fallback configuration is used. | ||
| // If configuration cannot be used to instantiate a logger, | ||
| // the same fallback configuration is used. | ||
| func NewLogger(configJSON string, levelOverride string) *zap.SugaredLogger { | ||
| func NewLogger(configJSON string, levelOverride string) (*zap.SugaredLogger, error) { | ||
| logger, err := newLoggerFromConfig(configJSON, levelOverride) | ||
| if err == nil { | ||
| return logger.Sugar() | ||
| return logger.Sugar(), nil | ||
| } | ||
|
|
||
| logger, err2 := zap.NewProduction() | ||
| if err2 != nil { | ||
| panic(err2) | ||
| } | ||
|
|
||
| logger.Error("Failed to parse the logging config. Falling back to default logger.", zap.Error(err), zap.String(logkey.JSONConfig, configJSON)) | ||
| return logger.Sugar() | ||
| return logger.Sugar(), fmt.Errorf("Failed to parse the logging config. Falling back to default logger. Error: %s. Config: %s", err, configJSON) | ||
| } | ||
|
|
||
| // NewLoggerFromDefaultConfigMap creates a logger using the configuration within /etc/config-logging file. | ||
| func NewLoggerFromDefaultConfigMap(logLevelKey string) *zap.SugaredLogger { | ||
| loggingFlagSet := k8sflag.NewFlagSet("/etc/config-logging") | ||
| // NewDefaultConfigMapLogger creates a logging object for the | ||
| // for the logLevelKey within configPath corresponding | ||
| // to the provided componentKey. | ||
| func NewDefaultConfigMapLogger(configPath string, componentKey string) (*zap.SugaredLogger, error) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given there is a default config path we can simplify the usability of this function a bit more by making configPath optional. Maybe it makes sense to name this function ie. then the usage would simplified to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for letting me know about this! i didn't know about variadic functions in Go! however after looking into it a bit, I'm not sure I like the idea of using them for this optional params and here is an example of why. The TL;DidntRun is the fact that using a variadic args means callers can start passing you incorrect arguments have you have to either check and make sure they're not doing that explicitly or allow the weird extra arguments to silently not do anything, and neither option seems great
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup it's dirty - would you settle on having two ie. func NewComponentLogger(componentKey string) (*zap.SugaredLogger, error) {
return NewComponentLoggerFromConfig(componentKey, DefaultLoggingConfigPath)
}
func NewComponentLoggerFromConfig(componentKey, configPath string) (*zap.SugaredLogger, error) {
...
} |
||
| logLevelKey := "loglevel." + componentKey | ||
|
|
||
| loggingFlagSet := k8sflag.NewFlagSet(configPath) | ||
| fmt.Println(loggingFlagSet) | ||
| zapCfg := loggingFlagSet.String("zap-logger-config", "") | ||
| zapLevelOverride := loggingFlagSet.String(logLevelKey, "") | ||
| return NewLogger(zapCfg.Get(), zapLevelOverride.Get()) | ||
|
|
||
| genericLogger, err := NewLogger(zapCfg.Get(), zapLevelOverride.Get()) | ||
| logger := genericLogger.Named(componentKey) | ||
| return logger, err | ||
| } | ||
|
|
||
| func newLoggerFromConfig(configJSON string, levelOverride string) (*zap.Logger, error) { | ||
|
|
@@ -65,6 +76,7 @@ func newLoggerFromConfig(configJSON string, levelOverride string) (*zap.Logger, | |
|
|
||
| if len(levelOverride) > 0 { | ||
| var level zapcore.Level | ||
| // If the levelOverride can't be parsed, we just ignore it and continue. | ||
| if err := level.Set(levelOverride); err == nil { | ||
| loggingCfg.Level = zap.NewAtomicLevelAt(level) | ||
| } | ||
|
|
@@ -75,7 +87,5 @@ func newLoggerFromConfig(configJSON string, levelOverride string) (*zap.Logger, | |
| return nil, err | ||
| } | ||
|
|
||
| logger.Info("Successfully created the logger.", zap.String(logkey.JSONConfig, configJSON)) | ||
| logger.Sugar().Infof("Logging level set to %v", loggingCfg.Level) | ||
| return logger, nil | ||
| return logger, err | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattmoor let me know if you wanted me to do something more specific re 'not relying on /etc/config-logging'