From 733f467513f0d43be35059cd539eee7291974ff3 Mon Sep 17 00:00:00 2001 From: leovct Date: Fri, 10 Nov 2023 09:51:26 +0100 Subject: [PATCH 1/7] chore: remove verbosity hack for `polycli monitor` --- cmd/monitor/cmd.go | 7 +++++ cmd/root.go | 56 ++++---------------------------------- util/loog.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 50 deletions(-) create mode 100644 util/loog.go diff --git a/cmd/monitor/cmd.go b/cmd/monitor/cmd.go index 2f57cd04..c1f87501 100644 --- a/cmd/monitor/cmd.go +++ b/cmd/monitor/cmd.go @@ -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, remove logs from `polycli monitor`. + verbosityFlag := cmd.Flags().Lookup("verbosity") + if verbosityFlag != nil && !verbosityFlag.Changed { + util.SetLogLevel(int(util.Silent)) + } + }, PreRunE: func(cmd *cobra.Command, args []string) error { return checkFlags() }, diff --git a/cmd/root.go b/cmd/root.go index 8d385dc8..ecb67f90 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,10 +83,12 @@ 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) + util.SetLogLevel(verbosity) + + if pretty { + util.SetLogMode(util.Console) } else { - setLogLevel(verbosity, pretty) + util.SetLogMode(util.JSON) } }, } @@ -138,33 +124,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/loog.go b/util/loog.go new file mode 100644 index 00000000..716040f7 --- /dev/null +++ b/util/loog.go @@ -0,0 +1,68 @@ +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) + } +} + +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 +} From f64e4bb706749aac81d1b1540f81c9a0f51c3878 Mon Sep 17 00:00:00 2001 From: leovct Date: Fri, 10 Nov 2023 10:04:46 +0100 Subject: [PATCH 2/7] chore: lint and clean up --- cmd/monitor/cmd.go | 5 +++-- cmd/root.go | 12 +++++++----- util/{loog.go => log.go} | 5 +++-- 3 files changed, 13 insertions(+), 9 deletions(-) rename util/{loog.go => log.go} (93%) diff --git a/cmd/monitor/cmd.go b/cmd/monitor/cmd.go index c1f87501..7d8c3fa1 100644 --- a/cmd/monitor/cmd.go +++ b/cmd/monitor/cmd.go @@ -26,12 +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) { + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { // By default, remove logs from `polycli monitor`. verbosityFlag := cmd.Flags().Lookup("verbosity") if verbosityFlag != nil && !verbosityFlag.Changed { - util.SetLogLevel(int(util.Silent)) + return util.SetLogLevel(int(util.Silent)) } + return nil }, PreRunE: func(cmd *cobra.Command, args []string) error { return checkFlags() diff --git a/cmd/root.go b/cmd/root.go index ecb67f90..7732fb0d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -82,14 +82,16 @@ 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) { - util.SetLogLevel(verbosity) + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if err := util.SetLogLevel(verbosity); err != nil { + return err + } + logMode := util.JSON if pretty { - util.SetLogMode(util.Console) - } else { - util.SetLogMode(util.JSON) + logMode = util.Console } + return util.SetLogMode(logMode) }, } diff --git a/util/loog.go b/util/log.go similarity index 93% rename from util/loog.go rename to util/log.go index 716040f7..855c67d5 100644 --- a/util/loog.go +++ b/util/log.go @@ -25,7 +25,7 @@ const ( // SetLogLevel sets the log level based on the flags. // https://logging.apache.org/log4j/2.x/manual/customloglevels.html -func SetLogLevel(verbosity int) { +func SetLogLevel(verbosity int) error { switch { case verbosity == int(Silent): zerolog.SetGlobalLevel(zerolog.NoLevel) @@ -42,8 +42,9 @@ func SetLogLevel(verbosity int) { case verbosity < int(Debug): zerolog.SetGlobalLevel(zerolog.DebugLevel) default: - zerolog.SetGlobalLevel(zerolog.TraceLevel) + return fmt.Errorf("unsupported log level: %d", verbosity) } + return nil } type LogMode string From f476f61e6e30db2ffb0417d1c036e6558244f818 Mon Sep 17 00:00:00 2001 From: leovct Date: Fri, 10 Nov 2023 10:07:07 +0100 Subject: [PATCH 3/7] chore: add trace level --- util/log.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/util/log.go b/util/log.go index 855c67d5..81a0a7b7 100644 --- a/util/log.go +++ b/util/log.go @@ -41,6 +41,8 @@ func SetLogLevel(verbosity int) error { zerolog.SetGlobalLevel(zerolog.InfoLevel) case verbosity < int(Debug): zerolog.SetGlobalLevel(zerolog.DebugLevel) + case verbosity < int(Trace): + zerolog.SetGlobalLevel(zerolog.TraceLevel) default: return fmt.Errorf("unsupported log level: %d", verbosity) } From 939f43cddf7b594fac80215cce7aca2cf1888369 Mon Sep 17 00:00:00 2001 From: leovct Date: Fri, 10 Nov 2023 10:11:36 +0100 Subject: [PATCH 4/7] chore: clean up --- cmd/monitor/cmd.go | 5 ++--- cmd/root.go | 5 +---- util/log.go | 7 ++----- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/cmd/monitor/cmd.go b/cmd/monitor/cmd.go index 7d8c3fa1..c1f87501 100644 --- a/cmd/monitor/cmd.go +++ b/cmd/monitor/cmd.go @@ -26,13 +26,12 @@ var MonitorCmd = &cobra.Command{ Short: "Monitor blocks using a JSON-RPC endpoint.", Long: usage, Args: cobra.NoArgs, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PersistentPreRun: func(cmd *cobra.Command, args []string) { // By default, remove logs from `polycli monitor`. verbosityFlag := cmd.Flags().Lookup("verbosity") if verbosityFlag != nil && !verbosityFlag.Changed { - return util.SetLogLevel(int(util.Silent)) + util.SetLogLevel(int(util.Silent)) } - return nil }, PreRunE: func(cmd *cobra.Command, args []string) error { return checkFlags() diff --git a/cmd/root.go b/cmd/root.go index 7732fb0d..d76d476f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -83,10 +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.", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if err := util.SetLogLevel(verbosity); err != nil { - return err - } - + util.SetLogLevel(verbosity) logMode := util.JSON if pretty { logMode = util.Console diff --git a/util/log.go b/util/log.go index 81a0a7b7..716040f7 100644 --- a/util/log.go +++ b/util/log.go @@ -25,7 +25,7 @@ const ( // SetLogLevel sets the log level based on the flags. // https://logging.apache.org/log4j/2.x/manual/customloglevels.html -func SetLogLevel(verbosity int) error { +func SetLogLevel(verbosity int) { switch { case verbosity == int(Silent): zerolog.SetGlobalLevel(zerolog.NoLevel) @@ -41,12 +41,9 @@ func SetLogLevel(verbosity int) error { zerolog.SetGlobalLevel(zerolog.InfoLevel) case verbosity < int(Debug): zerolog.SetGlobalLevel(zerolog.DebugLevel) - case verbosity < int(Trace): - zerolog.SetGlobalLevel(zerolog.TraceLevel) default: - return fmt.Errorf("unsupported log level: %d", verbosity) + zerolog.SetGlobalLevel(zerolog.TraceLevel) } - return nil } type LogMode string From 0f26781b7d403bc66a827cc84dba56ef9f700d00 Mon Sep 17 00:00:00 2001 From: leovct Date: Fri, 10 Nov 2023 10:52:39 +0100 Subject: [PATCH 5/7] chore: nit --- cmd/monitor/cmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/monitor/cmd.go b/cmd/monitor/cmd.go index c1f87501..f8724064 100644 --- a/cmd/monitor/cmd.go +++ b/cmd/monitor/cmd.go @@ -27,7 +27,7 @@ var MonitorCmd = &cobra.Command{ Long: usage, Args: cobra.NoArgs, PersistentPreRun: func(cmd *cobra.Command, args []string) { - // By default, remove logs from `polycli monitor`. + // By default, hide logs from `polycli monitor`. verbosityFlag := cmd.Flags().Lookup("verbosity") if verbosityFlag != nil && !verbosityFlag.Changed { util.SetLogLevel(int(util.Silent)) From d366243d67bf8b2cd98024d7457d36469cda8d80 Mon Sep 17 00:00:00 2001 From: leovct Date: Fri, 10 Nov 2023 10:53:52 +0100 Subject: [PATCH 6/7] chore: nit --- util/log.go | 1 + 1 file changed, 1 insertion(+) diff --git a/util/log.go b/util/log.go index 716040f7..d7168b38 100644 --- a/util/log.go +++ b/util/log.go @@ -46,6 +46,7 @@ func SetLogLevel(verbosity int) { } } +// LogMode represents the logger mode. type LogMode string const ( From 677184b5f9085507002653e2c2f78a9d0cf8a2e0 Mon Sep 17 00:00:00 2001 From: leovct Date: Tue, 14 Nov 2023 09:10:49 +0100 Subject: [PATCH 7/7] chore: use `cmd.Flag("verbosity")` --- cmd/monitor/cmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/monitor/cmd.go b/cmd/monitor/cmd.go index f8724064..55f8cfe4 100644 --- a/cmd/monitor/cmd.go +++ b/cmd/monitor/cmd.go @@ -28,7 +28,7 @@ var MonitorCmd = &cobra.Command{ Args: cobra.NoArgs, PersistentPreRun: func(cmd *cobra.Command, args []string) { // By default, hide logs from `polycli monitor`. - verbosityFlag := cmd.Flags().Lookup("verbosity") + verbosityFlag := cmd.Flag("verbosity") if verbosityFlag != nil && !verbosityFlag.Changed { util.SetLogLevel(int(util.Silent)) }