diff --git a/cmd/monitor/cmd.go b/cmd/monitor/cmd.go index 2f57cd04..79fc8804 100644 --- a/cmd/monitor/cmd.go +++ b/cmd/monitor/cmd.go @@ -26,6 +26,10 @@ var MonitorCmd = &cobra.Command{ Short: "Monitor blocks using a JSON-RPC endpoint.", Long: usage, Args: cobra.NoArgs, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + // Avoid logging error and warning messages to the screen by default. + util.SetLogLevel(int(util.Silent), false) + }, PreRunE: func(cmd *cobra.Command, args []string) error { return checkFlags() }, diff --git a/cmd/root.go b/cmd/root.go index 8d385dc8..042d1a43 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,8 +7,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" @@ -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 @@ -99,11 +83,7 @@ func NewPolycliCommand() *cobra.Command { 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) - } + util.SetLogLevel(verbosity, pretty) }, } @@ -138,33 +118,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") - } -} diff --git a/util/verbosity.go b/util/verbosity.go new file mode 100644 index 00000000..041f21c6 --- /dev/null +++ b/util/verbosity.go @@ -0,0 +1,53 @@ +package util + +import ( + "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, 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") + } +}