From 5197e89e7358ed42e9fb8171cfb7f7f6724cbe80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Mon, 27 May 2019 11:00:44 +0300 Subject: [PATCH 1/2] karma: add ability to configure log format Add a rudimentary switch which would let users switch between different formats used for logging. This is needed so that software such as `logstash` could easily understand the structure of log messages and index them accordingly. --- internal/config/config.go | 3 +++ internal/config/models.go | 1 + main.go | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/internal/config/config.go b/internal/config/config.go index 91d404b82..ee436f478 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -70,6 +70,8 @@ func init() { pflag.Bool("log.config", true, "Log used configuration to log on startup") pflag.String("log.level", "info", "Log level, one of: debug, info, warning, error, fatal and panic") + pflag.String("log.format", "text", + "Log format, one of: text, json") pflag.StringSlice("receivers.keep", []string{}, "List of receivers to keep, all alerts with different receivers will be ignored") @@ -159,6 +161,7 @@ func (config *configSchema) Read() { config.Listen.Prefix = v.GetString("listen.prefix") config.Log.Config = v.GetBool("log.config") config.Log.Level = v.GetString("log.level") + config.Log.Format = v.GetString("log.format") config.Receivers.Keep = v.GetStringSlice("receivers.keep") config.Receivers.Strip = v.GetStringSlice("receivers.strip") config.Sentry.Private = v.GetString("sentry.private") diff --git a/internal/config/models.go b/internal/config/models.go index f7831f818..1d3abf0cd 100644 --- a/internal/config/models.go +++ b/internal/config/models.go @@ -82,6 +82,7 @@ type configSchema struct { Log struct { Config bool Level string + Format string } JIRA []jiraRule Receivers struct { diff --git a/main.go b/main.go index 7fab3239a..6a8b726cb 100644 --- a/main.go +++ b/main.go @@ -158,6 +158,15 @@ func setupLogger() { default: log.Fatalf("Unknown log level '%s'", config.Config.Log.Level) } + + switch config.Config.Log.Format { + case "text": + log.SetFormatter(&log.TextFormatter{}) + case "json": + log.SetFormatter(&log.JSONFormatter{}) + default: + log.Fatalf("Unknown log format '%s'", config.Config.Log.Format) + } } func main() { From d0a10769623b763cd305ebab3cf7d4a55f3df56c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Mon, 27 May 2019 11:12:51 +0300 Subject: [PATCH 2/2] internal/config: fix tests Add a new field to the configuration string: log.format. It was missing thus the unit test fails. --- internal/config/config_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 882fd3f7a..2daa5086f 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -112,6 +112,7 @@ listen: log: config: true level: info + format: text jira: [] receivers: keep: []