Browse Source

jpictl: move verbosity handling to the log module

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/34/head
Alejandro Mery 1 year ago
parent
commit
0c5429a681
  1. 21
      cmd/jpictl/log.go
  2. 19
      cmd/jpictl/main.go

21
cmd/jpictl/log.go

@ -3,9 +3,13 @@ package main
import ( import (
"fmt" "fmt"
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog" "darvaza.org/slog"
"github.com/spf13/cobra"
) )
var log = zerolog.New(nil, slog.Error)
// fatal is a convenience wrapper for slog.Logger.Fatal().Print() // fatal is a convenience wrapper for slog.Logger.Fatal().Print()
func fatal(err error, msg string, args ...any) { func fatal(err error, msg string, args ...any) {
l := log.Fatal() l := log.Fatal()
@ -19,3 +23,20 @@ func fatal(err error, msg string, args ...any) {
panic("unreachable") panic("unreachable")
} }
var verbosity int
// setVerbosity replaces the global logger using the
// verbosity level specified via -v flags
func setVerbosity(_ *cobra.Command, _ []string) {
desired := int8(slog.Error) + int8(verbosity)
if desired > 6 {
desired = 6
}
log = zerolog.New(nil, slog.LogLevel(desired))
}
func init() {
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
"increase the verbosity level to Warn, Info or Debug")
}

19
cmd/jpictl/main.go

@ -2,8 +2,6 @@
package main package main
import ( import (
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -13,9 +11,7 @@ const (
) )
var ( var (
log = zerolog.New(nil, slog.Error) rootCmd = &cobra.Command{
verbosity int
rootCmd = &cobra.Command{
Use: CmdName, Use: CmdName,
Short: "control tool for jpi.cloud", Short: "control tool for jpi.cloud",
} }
@ -26,16 +22,3 @@ func main() {
fatal(err, "") fatal(err, "")
} }
} }
func init() {
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
"increase the verbosity level to Warn, Info or Debug")
}
func setVerbosity(_ *cobra.Command, _ []string) {
desired := int8(slog.Error) + int8(verbosity)
if desired > 6 {
desired = 6
}
log = zerolog.New(nil, slog.LogLevel(desired))
}

Loading…
Cancel
Save