From ee9d8e1b483cc01a4b562a3b16138d492c61ebaa Mon Sep 17 00:00:00 2001 From: TArch64 Date: Thu, 19 Sep 2024 19:38:25 +0300 Subject: [PATCH 1/3] add log level environment variable --- cmd/riverui/logger.go | 38 ++++++++++++++++++++++++++++++++++++++ cmd/riverui/main.go | 10 +--------- docs/README.md | 9 +++++++++ 3 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 cmd/riverui/logger.go diff --git a/cmd/riverui/logger.go b/cmd/riverui/logger.go new file mode 100644 index 00000000..76c70572 --- /dev/null +++ b/cmd/riverui/logger.go @@ -0,0 +1,38 @@ +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 { + if isDebug() { + 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 + } + + return slog.LevelInfo +} + +func isDebug() bool { + debugEnv := os.Getenv("RIVER_DEBUG") + return debugEnv == "1" || debugEnv == "true" +} diff --git a/cmd/riverui/main.go b/cmd/riverui/main.go index 0470695c..4d22a315 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 8286ea6b..452b48cf 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). From d0333a86061e244644e3f87b08f944c38ad2ff3b Mon Sep 17 00:00:00 2001 From: TArch64 Date: Sat, 21 Sep 2024 10:59:37 +0300 Subject: [PATCH 2/3] resolve comments --- cmd/riverui/logger.go | 12 ++++-------- docs/README.md | 8 ++++---- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/cmd/riverui/logger.go b/cmd/riverui/logger.go index 76c70572..5e4caa83 100644 --- a/cmd/riverui/logger.go +++ b/cmd/riverui/logger.go @@ -14,7 +14,8 @@ func initLogger() { } func getLogLevel() slog.Level { - if isDebug() { + debugEnv := os.Getenv("RIVER_DEBUG") + if debugEnv == "1" || debugEnv == "true" { return slog.LevelDebug } @@ -27,12 +28,7 @@ func getLogLevel() slog.Level { return slog.LevelWarn case "error": return slog.LevelError + default: + return slog.LevelInfo } - - return slog.LevelInfo -} - -func isDebug() bool { - debugEnv := os.Getenv("RIVER_DEBUG") - return debugEnv == "1" || debugEnv == "true" } diff --git a/docs/README.md b/docs/README.md index 452b48cf..9a1f6002 100644 --- a/docs/README.md +++ b/docs/README.md @@ -50,10 +50,10 @@ The `riverui` command accepts a `-prefix` arg to set a path prefix on both the A 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` +* `debug` +* `info` (default) +* `warn` +* `error` ## Development From 83ab5d68bc06dd1d16e1dc7321aa93dbe25e33d5 Mon Sep 17 00:00:00 2001 From: Blake Gentry Date: Sat, 21 Sep 2024 09:18:19 -0500 Subject: [PATCH 3/3] changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fc0fae3..13f5f7bb 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).