Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
enable optional JSON log output
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcz committed Jun 13, 2021
1 parent bfd3b07 commit 6d54eb9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ GLOBAL OPTIONS:
--ldapPass value OpenLDAP bind password (optional) [$LDAP_PASS]
--interval value Scrape interval (default: 30s) [$INTERVAL]
--webCfgFile FILE Prometheus metrics web config FILE (optional) [$WEB_CFG_FILE]
--jsonLog Output logs in JSON format (default: false) [$JSON_LOG]
--config YAML_FILE Optional configuration from a YAML_FILE
--help, -h show help (default: false)
--version, -v print the version (default: false)
Expand Down
15 changes: 14 additions & 1 deletion cmd/openldap_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
ldapPass = "ldapPass"
interval = "interval"
metrics = "metrPath"
jsonLog = "jsonLog"
webCfgFile = "webCfgFile"
config = "config"
)
Expand Down Expand Up @@ -74,6 +75,12 @@ func main() {
Usage: "Prometheus metrics web config `FILE` (optional)",
EnvVars: []string{"WEB_CFG_FILE"},
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: jsonLog,
Value: false,
Usage: "Output logs in JSON format",
EnvVars: []string{"JSON_LOG"},
}),
&cli.StringFlag{
Name: config,
Usage: "Optional configuration from a `YAML_FILE`",
Expand All @@ -88,7 +95,6 @@ func main() {
Flags: flags,
Action: runMain,
}
log.Info("service starting")
if err := app.Run(os.Args); err != nil {
log.WithError(err).Fatal("service failed")
}
Expand All @@ -106,6 +112,13 @@ func optionalYamlSourceFunc(flagFileName string) func(context *cli.Context) (alt
}

func runMain(c *cli.Context) error {
if c.Bool(jsonLog) {
log.SetFormatter(&log.JSONFormatter{})
} else {
log.SetFormatter(&log.TextFormatter{})
}
log.Info("service starting")

server := exporter.NewMetricsServer(
c.String(promAddr),
c.String(metrics),
Expand Down

0 comments on commit 6d54eb9

Please sign in to comment.