From 1b517b02b48197794c8162f4ac0f03bd8114db99 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Sun, 19 Nov 2023 15:11:12 +0000 Subject: [PATCH] feat(server): cmd flag to disable colored logs (backport #18478) (#18507) Co-authored-by: Robert Zaremba Co-authored-by: Julien Robert --- CHANGELOG.md | 1 + client/flags/flags.go | 5 +++-- server/cmd/execute.go | 1 + server/util.go | 7 ++++--- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 809057644030..b82120887099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (server) [#18478](https://github.com/cosmos/cosmos-sdk/pull/18478) Add command flag to disable colored logs. * (x/gov) [#18025](https://github.com/cosmos/cosmos-sdk/pull/18025) Improve ` q gov proposer` by querying directly a proposal instead of tx events. It is an alias of `q gov proposal` as the proposer is a field of the proposal. * (version) [#18063](https://github.com/cosmos/cosmos-sdk/pull/18063) Allow to define extra info to be displayed in ` version --long` command. diff --git a/client/flags/flags.go b/client/flags/flags.go index d939fabb6715..26a93a5e6e2f 100644 --- a/client/flags/flags.go +++ b/client/flags/flags.go @@ -86,8 +86,9 @@ const ( // This differs from FlagOutputDocument that is used to set the output file. FlagOutput = "output" // Logging flags - FlagLogLevel = "log_level" - FlagLogFormat = "log_format" + FlagLogLevel = "log_level" + FlagLogFormat = "log_format" + FlagLogNoColor = "log_no_color" ) // List of supported output formats diff --git a/server/cmd/execute.go b/server/cmd/execute.go index 874fc51cc102..cf8e7c009f44 100644 --- a/server/cmd/execute.go +++ b/server/cmd/execute.go @@ -28,6 +28,7 @@ func Execute(rootCmd *cobra.Command, envPrefix, defaultHome string) error { rootCmd.PersistentFlags().String(flags.FlagLogLevel, zerolog.InfoLevel.String(), "The logging level (trace|debug|info|warn|error|fatal|panic|disabled or '*:,:')") // NOTE: The default logger is only checking for the "json" value, any other value will default to plain text. rootCmd.PersistentFlags().String(flags.FlagLogFormat, "plain", "The logging format (json|plain)") + rootCmd.PersistentFlags().Bool(flags.FlagLogNoColor, false, "Disable colored logs") executor := cmtcli.PrepareBaseCmd(rootCmd, envPrefix, defaultHome) return executor.ExecuteContext(ctx) diff --git a/server/util.go b/server/util.go index f74ec00d6c01..080fa6df78d3 100644 --- a/server/util.go +++ b/server/util.go @@ -173,6 +173,10 @@ func CreateSDKLogger(ctx *Context, out io.Writer) (log.Logger, error) { if ctx.Viper.GetString(flags.FlagLogFormat) == flags.OutputFormatJSON { opts = append(opts, log.OutputJSONOption()) } + opts = append(opts, + log.ColorOption(!ctx.Viper.GetBool(flags.FlagLogNoColor)), + // We use CometBFT flag (cmtcli.TraceFlag) for trace logging. + log.TraceOption(ctx.Viper.GetBool(FlagTrace))) // check and set filter level or keys for the logger if any logLvlStr := ctx.Viper.GetString(flags.FlagLogLevel) @@ -194,9 +198,6 @@ func CreateSDKLogger(ctx *Context, out io.Writer) (log.Logger, error) { opts = append(opts, log.LevelOption(logLvl)) } - // Check if the CometBFT flag for trace logging is set and enable stack traces if so. - opts = append(opts, log.TraceOption(ctx.Viper.GetBool("trace"))) // cmtcli.TraceFlag - return log.NewLogger(out, opts...), nil }