Skip to content

Commit

Permalink
chore: remove verbosity hack for polycli monitor (#156)
Browse files Browse the repository at this point in the history
* chore: remove verbosity hack for `polycli monitor`

* chore: lint and clean up

* chore: add trace level

* chore: clean up

* chore: nit

* chore: nit

* chore: use `cmd.Flag("verbosity")`
  • Loading branch information
leovct authored Nov 14, 2023
1 parent f174e63 commit a49e238
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 52 deletions.
7 changes: 7 additions & 0 deletions cmd/monitor/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ var MonitorCmd = &cobra.Command{
Short: "Monitor blocks using a JSON-RPC endpoint.",
Long: usage,
Args: cobra.NoArgs,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// By default, hide logs from `polycli monitor`.
verbosityFlag := cmd.Flag("verbosity")
if verbosityFlag != nil && !verbosityFlag.Changed {
util.SetLogLevel(int(util.Silent))
}
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkFlags()
},
Expand Down
59 changes: 7 additions & 52 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"github.com/maticnetwork/polygon-cli/cmd/fork"
"github.com/maticnetwork/polygon-cli/cmd/p2p"
"github.com/maticnetwork/polygon-cli/cmd/parseethwallet"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/maticnetwork/polygon-cli/util"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -29,21 +28,6 @@ import (
"github.com/maticnetwork/polygon-cli/cmd/wallet"
)

// VerbosityLevel represents the verbosity levels.
// https://pkg.go.dev/github.com/rs/zerolog#readme-leveled-logging
type VerbosityLevel int

const (
Silent VerbosityLevel = 0
Panic VerbosityLevel = 100
Fatal VerbosityLevel = 200
Error VerbosityLevel = 300
Warn VerbosityLevel = 400
Info VerbosityLevel = 500
Debug VerbosityLevel = 600
Trace VerbosityLevel = 700
)

var (
cfgFile string
verbosity int
Expand Down Expand Up @@ -98,12 +82,13 @@ func NewPolycliCommand() *cobra.Command {
Use: "polycli",
Short: "A Swiss Army knife of blockchain tools.",
Long: "Polycli is a collection of tools that are meant to be useful while building, testing, and running block chain applications.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if cmd.Use == monitor.MonitorCmd.Use {
setLogLevel(int(Silent), pretty)
} else {
setLogLevel(verbosity, pretty)
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
util.SetLogLevel(verbosity)
logMode := util.JSON
if pretty {
logMode = util.Console
}
return util.SetLogMode(logMode)
},
}

Expand Down Expand Up @@ -138,33 +123,3 @@ func NewPolycliCommand() *cobra.Command {
)
return cmd
}

// setLogLevel sets the log level based on the flags.
// https://logging.apache.org/log4j/2.x/manual/customloglevels.html
func setLogLevel(verbosity int, pretty bool) {
switch {
case verbosity == int(Silent):
zerolog.SetGlobalLevel(zerolog.NoLevel)
case verbosity < int(Panic):
zerolog.SetGlobalLevel(zerolog.PanicLevel)
case verbosity < int(Fatal):
zerolog.SetGlobalLevel(zerolog.FatalLevel)
case verbosity < int(Error):
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case verbosity < int(Warn):
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case verbosity < int(Info):
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case verbosity < int(Debug):
zerolog.SetGlobalLevel(zerolog.DebugLevel)
default:
zerolog.SetGlobalLevel(zerolog.TraceLevel)
}

if pretty {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Debug().Msg("Starting logger in console mode")
} else {
log.Debug().Msg("Starting logger in JSON mode")
}
}
69 changes: 69 additions & 0 deletions util/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package util

import (
"fmt"
"os"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

// VerbosityLevel represents the verbosity levels.
// https://pkg.go.dev/github.com/rs/zerolog#readme-leveled-logging
type VerbosityLevel int

const (
Silent VerbosityLevel = 0
Panic VerbosityLevel = 100
Fatal VerbosityLevel = 200
Error VerbosityLevel = 300
Warn VerbosityLevel = 400
Info VerbosityLevel = 500
Debug VerbosityLevel = 600
Trace VerbosityLevel = 700
)

// SetLogLevel sets the log level based on the flags.
// https://logging.apache.org/log4j/2.x/manual/customloglevels.html
func SetLogLevel(verbosity int) {
switch {
case verbosity == int(Silent):
zerolog.SetGlobalLevel(zerolog.NoLevel)
case verbosity < int(Panic):
zerolog.SetGlobalLevel(zerolog.PanicLevel)
case verbosity < int(Fatal):
zerolog.SetGlobalLevel(zerolog.FatalLevel)
case verbosity < int(Error):
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case verbosity < int(Warn):
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case verbosity < int(Info):
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case verbosity < int(Debug):
zerolog.SetGlobalLevel(zerolog.DebugLevel)
default:
zerolog.SetGlobalLevel(zerolog.TraceLevel)
}
}

// LogMode represents the logger mode.
type LogMode string

const (
Console LogMode = "console"
JSON LogMode = "json"
)

// SetLogMode updates the log format.
func SetLogMode(mode LogMode) error {
switch mode {
case Console:
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Debug().Msg("Starting logger in console mode")
case JSON:
log.Debug().Msg("Starting logger in JSON mode")
default:
return fmt.Errorf("unsupported log mode: %s", mode)
}
return nil
}

0 comments on commit a49e238

Please sign in to comment.