Skip to content

Commit

Permalink
Add init that sets up logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tstirrat15 committed Aug 8, 2024
1 parent dfefc25 commit bae5e95
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package cmd
import (
"context"
"errors"
"io"
"os"
"os/signal"
"syscall"

"github.com/mattn/go-isatty"
"github.com/jzelinskie/cobrautil/v2"
"github.com/jzelinskie/cobrautil/v2/cobrazerolog"
"github.com/rs/zerolog"
Expand All @@ -21,6 +23,25 @@ var (
errParsing = errors.New("parsing error")
)

func init() {
// NOTE: this is mostly to set up logging in the case where
// the command doesn't exist or the construction of the command
// errors out before the PersistentPreRunE setup in the below function.
// It helps keep log output visually consistent for a user even in
// exceptional cases.
var output io.Writer

if isatty.IsTerminal(os.Stdout.Fd()) {
output = zerolog.ConsoleWriter{Out: os.Stderr}
} else {
output = os.Stderr
}

l := zerolog.New(output).With().Timestamp().Logger()

log.Logger = l
}

func Run() {
zl := cobrazerolog.New(cobrazerolog.WithPreRunLevel(zerolog.DebugLevel))

Expand Down

0 comments on commit bae5e95

Please sign in to comment.