Skip to content

Commit

Permalink
feat(logging): support custom time, level and message keys (#1295)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Jan 30, 2023
1 parent d0b365b commit 338c495
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/flipt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
10 changes: 10 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ func defaultConfig() *Config {
Level: "INFO",
Encoding: LogEncodingConsole,
GRPCLevel: "ERROR",
Keys: LogKeys{
Time: "T",
Level: "L",
Message: "M",
},
},

UI: UIConfig{
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions internal/config/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@ 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) {
v.SetDefault("log", map[string]any{
"level": "INFO",
"encoding": "console",
"grpc_level": "ERROR",
"keys": map[string]any{
"time": "T",
"level": "L",
"message": "M",
},
})
}

Expand Down
4 changes: 4 additions & 0 deletions internal/config/testdata/advanced.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ log:
level: WARN
file: "testLogFile.txt"
encoding: "json"
keys:
time: "time"
level: "level"
message: "msg"

cors:
enabled: true
Expand Down

0 comments on commit 338c495

Please sign in to comment.