Skip to content

Commit

Permalink
feat: configurable log level via cli
Browse files Browse the repository at this point in the history
Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Oct 12, 2023
1 parent 9376574 commit f6832b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions internal/cli/store/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"runtime/debug"
"syscall"

"github.com/brianmcgee/nvix/pkg/cli"

tvpb "code.tvl.fyi/tvix/store/protos"
"github.com/brianmcgee/nvix/pkg/pathinfo"

Expand All @@ -32,6 +34,8 @@ import (
)

type Run struct {
Log cli.LogOptions `embed:"" short:"v"`

NatsUrl string `short:"n" env:"NVIX_STORE_NATS_URL" default:"nats://localhost:4222"`
NatsCredentials string `short:"c" env:"NVIX_STORE_NATS_CREDENTIALS_FILE" required:"" type:"path"`

Expand All @@ -40,9 +44,7 @@ type Run struct {
}

func (r *Run) Run() error {
log.SetLevel(log.DebugLevel)

log.Debug("connecting to NATS", "url", r.NatsUrl, "creds", r.NatsCredentials)
r.Log.ConfigureLogger()

conn, err := nats.Connect(r.NatsUrl, nats.UserCredentials(r.NatsCredentials))
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions pkg/cli/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cli

import (
"github.com/charmbracelet/log"
)

type LogOptions struct {
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"NVIX_LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv"`
}

func (lo *LogOptions) ConfigureLogger() {
if lo.Verbosity == 0 {
log.SetLevel(log.WarnLevel)
} else if lo.Verbosity == 1 {
log.SetLevel(log.InfoLevel)
} else if lo.Verbosity >= 2 {
log.SetLevel(log.DebugLevel)
}
}

0 comments on commit f6832b8

Please sign in to comment.