Move the defaulting for ConfigMap into our logging library. (#258)

Progress towards: https://github.com/knative/serving/issues/2668
This commit is contained in:
Matt Moore 2019-01-30 21:59:37 -08:00 committed by Knative Prow Robot
parent da039ff041
commit 77b7b13190
2 changed files with 33 additions and 8 deletions

View File

@ -105,23 +105,48 @@ type Config struct {
LoggingLevel map[string]zapcore.Level
}
const defaultZLC = `{
"level": "info",
"development": false,
"outputPaths": ["stdout"],
"errorOutputPaths": ["stderr"],
"encoding": "json",
"encoderConfig": {
"timeKey": "ts",
"levelKey": "level",
"nameKey": "logger",
"callerKey": "caller",
"messageKey": "msg",
"stacktraceKey": "stacktrace",
"lineEnding": "",
"levelEncoder": "",
"timeEncoder": "iso8601",
"durationEncoder": "",
"callerEncoder": ""
}
}`
// NewConfigFromMap creates a LoggingConfig from the supplied map,
// expecting the given list of components.
func NewConfigFromMap(data map[string]string, components ...string) (*Config, error) {
lc := &Config{}
if zlc, ok := data["zap-logger-config"]; ok {
lc.LoggingConfig = zlc
} else {
lc.LoggingConfig = defaultZLC
}
lc.LoggingLevel = make(map[string]zapcore.Level)
for _, component := range components {
if ll, ok := data["loglevel."+component]; ok {
if len(ll) > 0 {
level, err := levelFromString(ll)
if err != nil {
return nil, err
}
lc.LoggingLevel[component] = *level
if ll := data["loglevel."+component]; len(ll) > 0 {
level, err := levelFromString(ll)
if err != nil {
return nil, err
}
lc.LoggingLevel[component] = *level
} else {
// We default components to INFO
lc.LoggingLevel[component] = zapcore.InfoLevel
}
}
return lc, nil

View File

@ -121,7 +121,7 @@ func TestNewConfigNoEntry(t *testing.T) {
if err != nil {
t.Errorf("Expected no errors. got: %v", err)
}
if got, want := c.LoggingConfig, ""; got != want {
if got, want := c.LoggingConfig, defaultZLC; got != want {
t.Errorf("LoggingConfig = %v, want %v", got, want)
}
if got, want := len(c.LoggingLevel), 0; got != want {