From 23ed4c9f9ebdddd3b11db7fd260d4709a9e63313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20=C3=98stergaard=20Jensen?= Date: Tue, 24 Sep 2024 12:55:58 +0200 Subject: [PATCH] Setup logrus and set log format This mostly matches the settings available for External DNS and sets up logrus which is used by External DNS for where the webhook server is called. --- README.md | 1 + cmd/webhook/logging.go | 13 ++++++++++--- cmd/webhook/main.go | 21 ++++++++++----------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f4dc9cf..2b1bbfa 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ The application arguments are as follows: - `tidydns-endpoint` Tidy DNS server addr - `zone-update-interval` The time-duration between updating the zone information - `log-level` Application logging level (debug, info, warn, error) +- `log-format` Application logging format (json or text) - `read-timeout` Read timeout in duration format (default: 5s) - `write-timeout` Write timeout in duration format (default: 10s) diff --git a/cmd/webhook/logging.go b/cmd/webhook/logging.go index 3a57fe1..9a73b59 100644 --- a/cmd/webhook/logging.go +++ b/cmd/webhook/logging.go @@ -27,17 +27,24 @@ const defaultLogLevel = slog.LevelInfo // one of (debug, info, warn, error), an out file where the log will be printet // to and the addSource boolean which when true will cause slog to print the // func, file and sourceline of the log call. -func loggingSetup(lvl string, out *os.File, addSource bool) *slog.Logger { +func loggingSetup(logFormat, logLevel string, out *os.File, addSource bool) *slog.Logger { programLevel := new(slog.LevelVar) handlerOpts := slog.HandlerOptions{ Level: programLevel, AddSource: addSource, } - logger := slog.New(slog.NewJSONHandler(out, &handlerOpts)) + var h slog.Handler + if logFormat == "json" { + h = slog.NewJSONHandler(out, &handlerOpts) + } else { + h = slog.NewTextHandler(out, &handlerOpts) + } + + logger := slog.New(h) slog.SetDefault(logger) - if err := programLevel.UnmarshalText([]byte(lvl)); err != nil { + if err := programLevel.UnmarshalText([]byte(logLevel)); err != nil { logger.Error(err.Error()) programLevel.Set(defaultLogLevel) } diff --git a/cmd/webhook/main.go b/cmd/webhook/main.go index 4a8e0af..c2c4c6e 100644 --- a/cmd/webhook/main.go +++ b/cmd/webhook/main.go @@ -26,14 +26,16 @@ import ( "github.com/neticdk/external-dns-tidydns-webhook/cmd/webhook/tidydns" "github.com/prometheus/client_golang/prometheus/promhttp" + log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/exporters/prometheus" "go.opentelemetry.io/otel/sdk/metric" "sigs.k8s.io/external-dns/provider/webhook/api" ) func main() { + logLevel := flag.String("log-level", "info", "Set the level of logging. (default: info, options: debug, info, warning, error)") + logFormat := flag.String("log-format", "text", "The format in which log messages are printed (default: text, options: text, json)") tidyEndpoint := flag.String("tidydns-endpoint", "", "DNS server address") - logLevel := flag.String("log-level", "", "logging level (debug, info, warn, err)") readTimeout := flag.Duration("read-timeout", (5 * time.Second), "Read timeout in duration format (default: 5s)") writeTimeout := flag.Duration("write-timeout", (10 * time.Second), "Write timeout in duration format (default: 10s)") @@ -45,19 +47,16 @@ func main() { tidyUsername := os.Getenv("TIDYDNS_USER") tidyPassword := os.Getenv("TIDYDNS_PASS") - // If log level isn't set as a parameter, read it from the environment - if *logLevel == "" { - *logLevel = os.Getenv("EXTERNAL_DNS_LOG_LEVEL") - } + // Setup the default slog logger + loggingSetup(*logFormat, *logLevel, os.Stderr, true) - // If neither the application parameter or the environment sets the log - // level, set default - if *logLevel == "" { - *logLevel = "info" + // External DNS uses logrus for logging, so we need to set that up as well + if *logFormat == "json" { + log.SetFormatter(&log.JSONFormatter{}) + } else { + log.SetFormatter(&log.TextFormatter{}) } - loggingSetup(*logLevel, os.Stderr, true) - // Print stachtraces with slog defer func() { if err := recover(); err != nil {