Skip to content

Commit

Permalink
add log level environment variable (#183)
Browse files Browse the repository at this point in the history
* add log level environment variable

* resolve comments

* changelog entry

---------

Co-authored-by: Blake Gentry <[email protected]>
  • Loading branch information
TArch64 and bgentry authored Sep 21, 2024
1 parent eaeaa33 commit b394a8e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
34 changes: 34 additions & 0 deletions cmd/riverui/logger.go
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 1 addition & 9 deletions cmd/riverui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
9 changes: 9 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

0 comments on commit b394a8e

Please sign in to comment.