From fb700bea4782c6905fb4835ad65b599e203f7ada Mon Sep 17 00:00:00 2001 From: Ashley Primo Date: Wed, 30 Oct 2019 10:45:16 +0000 Subject: [PATCH] Add -address listen, log -level flag --- ha_cluster_exporter.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/ha_cluster_exporter.go b/ha_cluster_exporter.go index 5e51291..c2659c9 100644 --- a/ha_cluster_exporter.go +++ b/ha_cluster_exporter.go @@ -73,15 +73,34 @@ func landingpage(w http.ResponseWriter, r *http.Request) { `)) } +func loglevel(opt string) { + switch opt { + case "error": + log.SetLevel(log.ErrorLevel) + case "warn": + log.SetLevel(log.WarnLevel) + case "info": + log.SetLevel(log.InfoLevel) + case "debug": + log.SetLevel(log.DebugLevel) + default: + log.Warnln("Unrecognized log level, default to `info` level") + } +} + var ( clock Clock = &SystemClock{} portNumber = flag.String("port", "9002", "The port number to listen on for HTTP requests.") + address = flag.String("address", "0.0.0.0", "The address to listen on for HTTP requests.") + logLevel = flag.String("level", "info", "The level of logs to log") ) func main() { // read cli option and setup initial stat flag.Parse() + loglevel(*logLevel) + pacemakerCollector, err := NewPacemakerCollector() if err != nil { log.Warnf("Could not initialize Pacemaker collector: %v\n", err) @@ -112,6 +131,6 @@ func main() { http.HandleFunc("/", landingpage) http.Handle("/metrics", promhttp.Handler()) - log.Infoln("Serving metrics on port", *portNumber) - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", *portNumber), nil)) + log.Infof("Serving metrics on %s:%s", *address, *portNumber) + log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", *address, *portNumber), nil)) }