diff --git a/exporter/collector.go b/exporter/collector.go index 6ede5af..bab2d29 100644 --- a/exporter/collector.go +++ b/exporter/collector.go @@ -3,7 +3,7 @@ package exporter import ( "context" "fmt" - "log" + "log/slog" "strconv" "github.com/digineo/triax-eoc-exporter/triax" @@ -72,7 +72,7 @@ func (t *triaxCollector) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric(ctrlUp, prometheus.GaugeValue, boolToFloat(err == nil)) if err != nil { - log.Println("fetching failed:", err) + slog.Error("fetching failed", "error", err) } } diff --git a/exporter/exporter.go b/exporter/exporter.go index c0cb5ae..ffa0427 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -5,7 +5,7 @@ import ( "encoding/json" "fmt" "io" - "log" + "log/slog" "net" "net/http" "text/template" @@ -30,8 +30,8 @@ func (cfg *Config) Start(listenAddress, version string) { router.GET("/controllers/:target/api/*path", cfg.targetMiddleware(cfg.apiHandler)) router.PUT("/controllers/:target/nodes/:mac", cfg.targetMiddleware(cfg.updateNodeHandler)) - log.Printf("Starting exporter on http://%s/", listenAddress) - log.Fatal(http.ListenAndServe(listenAddress, router)) + slog.Info("Starting exporter", "listenAddress", listenAddress) + slog.Info("Server stopped", "reason", http.ListenAndServe(listenAddress, router)) } type targetHandler func(*triax.Client, http.ResponseWriter, *http.Request, httprouter.Params) diff --git a/main.go b/main.go index 9531291..24c39e3 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,12 @@ package main import ( "fmt" "log" + "log/slog" + "os" "runtime/debug" - "github.com/digineo/triax-eoc-exporter/exporter" - "github.com/digineo/triax-eoc-exporter/triax" - kingpin "github.com/alecthomas/kingpin/v2" + "github.com/digineo/triax-eoc-exporter/exporter" ) // DefaultConfigPath points to the default config file location. @@ -44,15 +44,28 @@ func main() { kingpin.HelpFlag.Short('h') kingpin.Parse() + initLogger(*verbose) + cfg, err := exporter.LoadConfig(*configFile) if err != nil { log.Fatal(err.Error()) } - triax.Verbose = *verbose cfg.Start(*listenAddress, version) } +func initLogger(verbose bool) { + opts := slog.HandlerOptions{ + Level: slog.LevelInfo, + } + if verbose { + opts.Level = slog.LevelDebug + } + + logger := slog.New(slog.NewTextHandler(os.Stdout, &opts)) + slog.SetDefault(logger) +} + func printVersion() { info, ok := debug.ReadBuildInfo() if !ok { diff --git a/triax/client.go b/triax/client.go index ae002a7..164dbaa 100644 --- a/triax/client.go +++ b/triax/client.go @@ -8,8 +8,7 @@ import ( "errors" "fmt" "io" - "io/ioutil" - "log" + "log/slog" "net/http" "net/http/cookiejar" "net/url" @@ -23,14 +22,9 @@ type Client struct { password string } -var ( - // Verbose increases verbosity. - Verbose bool - - HTTPClient = http.Client{ - Timeout: time.Second * 30, - } -) +var HTTPClient = http.Client{ + Timeout: time.Second * 30, +} func init() { HTTPClient.Jar, _ = cookiejar.New(nil) // error is always nil @@ -123,9 +117,7 @@ retry: func (c *Client) apiRequestRaw(ctx context.Context, method, path string, request, response interface{}) error { url := fmt.Sprintf("%s://%s/api/%s", c.endpoint.Scheme, c.endpoint.Host, strings.TrimPrefix(path, "/")) - if Verbose { - log.Printf("%s %s", method, url) - } + slog.Info("HTTP Request", "method", method, "url", url) var body io.Reader if request != nil { @@ -153,7 +145,7 @@ func (c *Client) apiRequestRaw(ctx context.Context, method, path string, request defer res.Body.Close() if res.StatusCode != http.StatusOK { - data, _ := ioutil.ReadAll(res.Body) + data, _ := io.ReadAll(res.Body) return &ErrUnexpectedStatus{ Method: method, @@ -163,19 +155,18 @@ func (c *Client) apiRequestRaw(ctx context.Context, method, path string, request } } - jsonData, err := ioutil.ReadAll(res.Body) + jsonData, err := io.ReadAll(res.Body) if err != nil { return err } if response != nil { err = json.Unmarshal(jsonData, &response) - if Verbose || err != nil { - log.Println(string(jsonData)) - } if err != nil { return fmt.Errorf("decoding response failed: %w", err) } + + slog.Debug("response received", "json", string(jsonData)) } return nil