Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove verbosity hack for polycli monitor #156

Merged
merged 9 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.Flags().Lookup("verbosity")
leovct marked this conversation as resolved.
Show resolved Hide resolved
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
}