From b394a8e5dd549ab08218ef597c0e042d7fc026ee Mon Sep 17 00:00:00 2001 From: Taras Turchenko Date: Sat, 21 Sep 2024 17:25:29 +0300 Subject: [PATCH] add log level environment variable (#183) * add log level environment variable * resolve comments * changelog entry --------- Co-authored-by: Blake Gentry --- CHANGELOG.md | 4 ++++ cmd/riverui/logger.go | 34 ++++++++++++++++++++++++++++++++++ cmd/riverui/main.go | 10 +--------- docs/README.md | 9 +++++++++ 4 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 cmd/riverui/logger.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fc0fae..13f5f7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `RIVER_LOG_LEVEL` env for env-based configuration of River UI's log level. Thank you [Taras Turchenko](https://github.com/TArch64)! 🙏🏻 [PR #183](https://github.com/riverqueue/riverui/pull/183). + ### Changed - Allow `HOST` variable to specify specific host variable to bind to. [PR #157](https://github.com/riverqueue/riverui/pull/157). diff --git a/cmd/riverui/logger.go b/cmd/riverui/logger.go new file mode 100644 index 0000000..5e4caa8 --- /dev/null +++ b/cmd/riverui/logger.go @@ -0,0 +1,34 @@ +package main + +import ( + "log/slog" + "os" + "strings" +) + +var logger *slog.Logger //nolint:gochecknoglobals + +func initLogger() { + options := &slog.HandlerOptions{Level: getLogLevel()} + logger = slog.New(slog.NewTextHandler(os.Stdout, options)) +} + +func getLogLevel() slog.Level { + debugEnv := os.Getenv("RIVER_DEBUG") + if debugEnv == "1" || debugEnv == "true" { + return slog.LevelDebug + } + + env := strings.ToLower(os.Getenv("RIVER_LOG_LEVEL")) + + switch env { + case "debug": + return slog.LevelDebug + case "warn": + return slog.LevelWarn + case "error": + return slog.LevelError + default: + return slog.LevelInfo + } +} diff --git a/cmd/riverui/main.go b/cmd/riverui/main.go index 0470695..4d22a31 100644 --- a/cmd/riverui/main.go +++ b/cmd/riverui/main.go @@ -23,17 +23,9 @@ import ( "riverqueue.com/riverui" ) -var logger *slog.Logger //nolint:gochecknoglobals - func main() { ctx := context.Background() - - if os.Getenv("RIVER_DEBUG") == "1" || os.Getenv("RIVER_DEBUG") == "true" { - logger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})) - } else { - logger = slog.New(slog.NewTextHandler(os.Stdout, nil)) - } - + initLogger() os.Exit(initAndServe(ctx)) } diff --git a/docs/README.md b/docs/README.md index 8286ea6..9a1f600 100644 --- a/docs/README.md +++ b/docs/README.md @@ -46,6 +46,15 @@ $ docker run -p 8080:8080 --env DATABASE_URL ghcr.io/riverqueue/riverui:latest The `riverui` command accepts a `-prefix` arg to set a path prefix on both the API and static assets. When executing the Docker image, this is accepted as a `PATH_PREFIX` env. +### Logging Configuration + +The `riverui` command utilizes the `RIVER_LOG_LEVEL` environment variable to configure its logging level. The following values are accepted: + +* `debug` +* `info` (default) +* `warn` +* `error` + ## Development See [developing River UI](./development.md).