From 21f3929e6d50fb8710913911f30c77085efe597e Mon Sep 17 00:00:00 2001 From: George MacRorie Date: Fri, 27 Jan 2023 12:40:10 +0000 Subject: [PATCH] feat(logging): support custom time, level and message keys --- cmd/flipt/main.go | 3 +++ internal/config/config_test.go | 10 ++++++++++ internal/config/log.go | 12 ++++++++++++ internal/config/testdata/advanced.yml | 4 ++++ 4 files changed, 29 insertions(+) diff --git a/cmd/flipt/main.go b/cmd/flipt/main.go index 2c8b2f0f5a..74fbe94956 100644 --- a/cmd/flipt/main.go +++ b/cmd/flipt/main.go @@ -164,6 +164,9 @@ func main() { cfg = res.Config cfgWarnings = res.Warnings + loggerConfig.EncoderConfig.TimeKey = cfg.Log.Keys.Time + loggerConfig.EncoderConfig.LevelKey = cfg.Log.Keys.Level + loggerConfig.EncoderConfig.MessageKey = cfg.Log.Keys.Message // log to file if enabled if cfg.Log.File != "" { loggerConfig.OutputPaths = []string{cfg.Log.File} diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 036644d6b7..2ce1569493 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -168,6 +168,11 @@ func defaultConfig() *Config { Level: "INFO", Encoding: LogEncodingConsole, GRPCLevel: "ERROR", + Keys: LogKeys{ + Time: "T", + Level: "L", + Message: "M", + }, }, UI: UIConfig{ @@ -424,6 +429,11 @@ func TestLoad(t *testing.T) { File: "testLogFile.txt", Encoding: LogEncodingJSON, GRPCLevel: "ERROR", + Keys: LogKeys{ + Time: "time", + Level: "level", + Message: "msg", + }, } cfg.Cors = CorsConfig{ Enabled: true, diff --git a/internal/config/log.go b/internal/config/log.go index a1425f06d8..e895af6d2e 100644 --- a/internal/config/log.go +++ b/internal/config/log.go @@ -16,6 +16,13 @@ type LogConfig struct { File string `json:"file,omitempty" mapstructure:"file"` Encoding LogEncoding `json:"encoding,omitempty" mapstructure:"encoding"` GRPCLevel string `json:"grpcLevel,omitempty" mapstructure:"grpc_level"` + Keys LogKeys `json:"keys" mapstructure:"keys"` +} + +type LogKeys struct { + Time string `json:"time" mapstructure:"time"` + Level string `json:"level" mapstructure:"level"` + Message string `json:"message" mapstructure:"message"` } func (c *LogConfig) setDefaults(v *viper.Viper) { @@ -23,6 +30,11 @@ func (c *LogConfig) setDefaults(v *viper.Viper) { "level": "INFO", "encoding": "console", "grpc_level": "ERROR", + "keys": map[string]any{ + "time": "T", + "level": "L", + "message": "M", + }, }) } diff --git a/internal/config/testdata/advanced.yml b/internal/config/testdata/advanced.yml index e1efbe1339..5a868d4d48 100644 --- a/internal/config/testdata/advanced.yml +++ b/internal/config/testdata/advanced.yml @@ -2,6 +2,10 @@ log: level: WARN file: "testLogFile.txt" encoding: "json" + keys: + time: "time" + level: "level" + message: "msg" cors: enabled: true