Skip to content
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

feat(logging): support custom time, level and message keys #1295

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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