adds a flag for more readable console logs (#1642)

## Description

I was getting frustrated by the readability of logs
in the console, and wanted reduce some of my
eye strain during development.
Adds a hidden "readable-logs" flag for ease of
reading logs during development.

## Type of change

- [x] 🐹 Trivial/Minor

Logs without flag:
<img width="586" alt="image" src="https://user-images.githubusercontent.com/104464746/204873851-acd368d1-2c9c-43ef-bd4b-535896e56ef0.png">

Logs with flag:
<img width="406" alt="image" src="https://user-images.githubusercontent.com/104464746/204873940-8ab3c6fd-24f8-458e-8ea9-8bdd1c27f872.png">
This commit is contained in:
Keepers 2022-12-01 21:57:51 -07:00 committed by GitHub
parent 34ba06cf61
commit c04b3ac6cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,8 @@ var (
// logging level flag // logging level flag
// TODO: infer default based on environment. // TODO: infer default based on environment.
llFlag = "info" llFlag = "info"
readableOutput bool
) )
type logLevel int type logLevel int
@ -28,7 +30,10 @@ const (
Disabled Disabled
) )
const logLevelFN = "log-level" const (
logLevelFN = "log-level"
readableLogsFN = "readable-logs"
)
// adds the persistent flag --log-level to the provided command. // adds the persistent flag --log-level to the provided command.
// defaults to "info". // defaults to "info".
@ -37,6 +42,12 @@ const logLevelFN = "log-level"
func AddLogLevelFlag(parent *cobra.Command) { func AddLogLevelFlag(parent *cobra.Command) {
fs := parent.PersistentFlags() fs := parent.PersistentFlags()
fs.StringVar(&llFlag, logLevelFN, "info", "set the log level to debug|info|warn|error") fs.StringVar(&llFlag, logLevelFN, "info", "set the log level to debug|info|warn|error")
fs.Bool(
readableLogsFN, false,
"minimizes log output for console readability: removes the file and date, colors the level")
//nolint:errcheck
fs.MarkHidden(readableLogsFN)
} }
// Due to races between the lazy evaluation of flags in cobra and the need to init logging // Due to races between the lazy evaluation of flags in cobra and the need to init logging
@ -46,6 +57,7 @@ func PreloadLogLevel() string {
fs := pflag.NewFlagSet("seed-logger", pflag.ContinueOnError) fs := pflag.NewFlagSet("seed-logger", pflag.ContinueOnError)
fs.ParseErrorsWhitelist.UnknownFlags = true fs.ParseErrorsWhitelist.UnknownFlags = true
fs.String(logLevelFN, "info", "set the log level to debug|info|warn|error") fs.String(logLevelFN, "info", "set the log level to debug|info|warn|error")
fs.BoolVar(&readableOutput, readableLogsFN, false, "minimizes log output: removes the file and date, colors the level")
// prevents overriding the corso/cobra help processor // prevents overriding the corso/cobra help processor
fs.BoolP("help", "h", false, "") fs.BoolP("help", "h", false, "")
@ -111,7 +123,15 @@ func genLogger(level logLevel) (*zapcore.Core, *zap.SugaredLogger) {
cfg.Level = zap.NewAtomicLevelAt(zapcore.FatalLevel) cfg.Level = zap.NewAtomicLevelAt(zapcore.FatalLevel)
} }
lgr, err = cfg.Build() opts := []zap.Option{}
if readableOutput {
opts = append(opts, zap.WithCaller(false), zap.AddStacktrace(zapcore.DPanicLevel))
cfg.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("15:04:05.00")
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
lgr, err = cfg.Build(opts...)
} else { } else {
lgr, err = zap.NewProduction() lgr, err = zap.NewProduction()
} }