diff --git a/README.md b/README.md index f4dc9cf..5e4884d 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,12 @@ 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) +<<<<<<< HEAD - `read-timeout` Read timeout in duration format (default: 5s) - `write-timeout` Write timeout in duration format (default: 10s) +======= +- `log-format` Application logging format (json or text) +>>>>>>> fb3c360 (Setup logrus and set log format) This application is strictly meant to run in a container as a sidecar to External-DNS inside a Kubernetes environment. Refer to the External-DNS 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 {