From 7b347ebad2e3343d23d2d48e567e283191e5ca71 Mon Sep 17 00:00:00 2001 From: Tobias Germer Date: Mon, 13 Jun 2022 11:24:10 +0200 Subject: [PATCH 1/2] add support for log formats --- cmd/gitlab-ci-crawler/main.go | 14 ++++++++++++-- internal/crawler/config.go | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/gitlab-ci-crawler/main.go b/cmd/gitlab-ci-crawler/main.go index 1f10081..f16977f 100644 --- a/cmd/gitlab-ci-crawler/main.go +++ b/cmd/gitlab-ci-crawler/main.go @@ -15,9 +15,19 @@ var cfg crawler.Config var neo4jcfg neo4j.Config func init() { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout}) if err := crawler.ParseConfig(&cfg); err != nil { - log.Fatal().Err(err).Msg("failed to configure crawler") + log.Fatal().Err(err).Msg("failed to parse crawler config") + } + + switch cfg.LogFormat { + case "text": + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout}) + case "json": + log.Logger = log.Output(os.Stdout) + default: + log.Fatal(). + Str("LogFormat", cfg.LogFormat). + Msg("unsupported log format") } zerolog.SetGlobalLevel(zerolog.Level(cfg.LogLevel)) diff --git a/internal/crawler/config.go b/internal/crawler/config.go index 96f5e6b..28483a6 100644 --- a/internal/crawler/config.go +++ b/internal/crawler/config.go @@ -43,7 +43,8 @@ type Config struct { HTTPClientTimeout time.Duration `conf:"default:5s,short:x,env:HTTP_CLIENT_TIMEOUT"` // There should be global config composition maybe? For not this lives here // though this is the global log level - LogLevel int `conf:"default:1,env:LOG_LEVEL"` + LogLevel int `conf:"default:1,env:LOG_LEVEL"` + LogFormat string `conf:"default:text,env:LOG_FORMAT"` } func ParseConfig(cfg *Config) error { From 1900269700f4373016f4157de06049b28a0bb44a Mon Sep 17 00:00:00 2001 From: Tobias Germer Date: Mon, 13 Jun 2022 11:48:38 +0200 Subject: [PATCH 2/2] change default log format to json --- internal/crawler/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/crawler/config.go b/internal/crawler/config.go index 28483a6..6b49339 100644 --- a/internal/crawler/config.go +++ b/internal/crawler/config.go @@ -44,7 +44,7 @@ type Config struct { // There should be global config composition maybe? For not this lives here // though this is the global log level LogLevel int `conf:"default:1,env:LOG_LEVEL"` - LogFormat string `conf:"default:text,env:LOG_FORMAT"` + LogFormat string `conf:"default:json,env:LOG_FORMAT"` } func ParseConfig(cfg *Config) error {