Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 23 additions & 7 deletions cmd/slo_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,29 @@ 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
}

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
}
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```

Expand Down