Skip to content

Commit

Permalink
chore: lint and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct committed Nov 10, 2023
1 parent 733f467 commit f64e4bb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
5 changes: 3 additions & 2 deletions cmd/monitor/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 7 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
5 changes: 3 additions & 2 deletions util/loog.go → util/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit f64e4bb

Please sign in to comment.