-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove verbosity hack for
polycli monitor
(#156)
* chore: remove verbosity hack for `polycli monitor` * chore: lint and clean up * chore: add trace level * chore: clean up * chore: nit * chore: nit * chore: use `cmd.Flag("verbosity")`
- Loading branch information
Showing
3 changed files
with
83 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |