diff --git a/CHANGELOG.md b/CHANGELOG.md index 383e6f4..7a2bf5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased +- [#111](https://github.com/seznam/slo-exporter/pull/111) Add JSON logging support ## [v6.14.0] 2024-01-22 ### Changed diff --git a/cmd/slo_exporter.go b/cmd/slo_exporter.go index 5a8a265..eefd5ba 100644 --- a/cmd/slo_exporter.go +++ b/cmd/slo_exporter.go @@ -94,7 +94,7 @@ func moduleFactory(moduleName string, logger logrus.FieldLogger, conf *viper.Vip } } -func setupLogger(logLevel string) (*logrus.Logger, error) { +func setupLogger(logLevel string, logFormat string) (*logrus.Logger, error) { lvl, err := logrus.ParseLevel(logLevel) if err != nil { return nil, err @@ -102,11 +102,21 @@ func setupLogger(logLevel string) (*logrus.Logger, error) { newLogger := logrus.New() newLogger.SetOutput(os.Stdout) - newLogger.SetFormatter(&logrus.TextFormatter{ - DisableColors: true, - FullTimestamp: true, - TimestampFormat: "2006-01-02T15:04:05.99999Z07:00", - }) + const timestampFormat = "2006-01-02T15:04:05.99999Z07:00" + switch logFormat { + case "json": + newLogger.SetFormatter(&logrus.JSONFormatter{ + TimestampFormat: timestampFormat, + }) + case "text": + newLogger.SetFormatter(&logrus.TextFormatter{ + DisableColors: true, + FullTimestamp: true, + TimestampFormat: timestampFormat, + }) + default: + return nil, fmt.Errorf("invalid log format '%s', must be 'json' or 'text'", logFormat) + } newLogger.SetLevel(lvl) return newLogger, nil } @@ -147,6 +157,7 @@ func main() { configFilePath := kingpin.Flag("config-file", "SLO exporter configuration file.").ExistingFile() logLevel := kingpin.Flag("log-level", "Log level (error, warn, info, debug,trace).").Default("info").String() + logFormat := kingpin.Flag("log-format", "Log format (text, json).").Default("text").String() checkConfig := kingpin.Flag("check-config", "Only check config file and exit with 0 if ok and other status code if not.").Default("false").Bool() versionFlag := kingpin.Flag("version", "Display version.").Default("false").Bool() kingpin.Parse() @@ -167,7 +178,12 @@ func main() { logLevel = &envLogLevel } - logger, err := setupLogger(*logLevel) + envLogFormat, ok := syscall.Getenv("SLO_EXPORTER_LOGFORMAT") + if ok { + logFormat = &envLogFormat + } + + logger, err := setupLogger(*logLevel, *logFormat) if err != nil { log.Fatalf("invalid specified log level %+v, error: %+v", logLevel, err) } diff --git a/docs/configuration.md b/docs/configuration.md index c7a8ddf..911eb36 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -19,6 +19,7 @@ Flags: --help Show context-sensitive help (also try --help-long and --help-man). --config-file=CONFIG-FILE SLO exporter configuration file. --log-level="info" Log level (error, warn, info, debug,trace). + --log-format="text" Log format (text, json). --check-config Only check config file and exit with 0 if ok and other status code if not. ```