Skip to content

Commit

Permalink
chore: remove hack
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct committed Nov 9, 2023
1 parent 583abb4 commit 86de636
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 52 deletions.
4 changes: 4 additions & 0 deletions cmd/monitor/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
Expand Down
54 changes: 2 additions & 52 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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 @@ -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)
},
}

Expand Down Expand Up @@ -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")
}
}
53 changes: 53 additions & 0 deletions util/verbosity.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 86de636

Please sign in to comment.