Skip to content

Commit

Permalink
Setup logrus and set log format
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hatrx committed Sep 24, 2024
1 parent d4b95aa commit 31f2163
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions cmd/webhook/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
21 changes: 10 additions & 11 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")

Expand All @@ -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 {
Expand Down

0 comments on commit 31f2163

Please sign in to comment.