From dfab281e2e0bbc070c8f05a734b24cf594479e9b Mon Sep 17 00:00:00 2001 From: Artur Troian Date: Wed, 26 Apr 2023 10:47:07 -0400 Subject: [PATCH] feat(log): extend logger options - add option to enable/disable colored output when consoleWriter being used (non JSON output) - add option to customize/disable log timestamps new options do not change default behavior, but give coller ability to fine tune log configuration disable color and timestamps is handy when service like cosmovisor or cosmos-sdk based node runs in kubernetes environment, as kubernetes does not colors and output looks cluttered. Signed-off-by: Artur Troian --- log/CHANGELOG.md | 4 ++++ log/logger.go | 14 ++++++++++---- log/options.go | 44 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/log/CHANGELOG.md b/log/CHANGELOG.md index f393ecc080f9..08768555e71d 100644 --- a/log/CHANGELOG.md +++ b/log/CHANGELOG.md @@ -31,6 +31,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Features + +* [#15956](https://github.com/cosmos/cosmos-sdk/pull/15956) Introduce extra options to configure logger. + ## [v1.0.0](https://github.com/cosmos/cosmos-sdk/releases/tag/log/v1.0.0) - 2023-03-30 * [#15601](https://github.com/cosmos/cosmos-sdk/pull/15601) Introduce logger options. These options allow to configure the logger with filters, different level and output format. diff --git a/log/logger.go b/log/logger.go index da1552ef6a01..46ae75b6198c 100644 --- a/log/logger.go +++ b/log/logger.go @@ -2,12 +2,11 @@ package log import ( "io" - "time" "github.com/rs/zerolog" ) -// Defines commons keys for logging. +// ModuleKey defines a module logging key. const ModuleKey = "module" // ContextKey is used to store the logger in the context. @@ -58,14 +57,21 @@ func NewLogger(dst io.Writer, options ...Option) Logger { output := dst if !logCfg.OutputJSON { - output = zerolog.ConsoleWriter{Out: dst, TimeFormat: time.Kitchen} + output = zerolog.ConsoleWriter{ + Out: dst, + NoColor: !logCfg.Color, + TimeFormat: logCfg.TimeFormat, + } } if logCfg.Filter != nil { output = NewFilterWriter(output, logCfg.Filter) } - logger := zerolog.New(output).With().Timestamp().Logger() + logger := zerolog.New(output) + if logCfg.TimeFormat != "" { + logger = logger.With().Timestamp().Logger() + } if logCfg.Level != zerolog.NoLevel { logger = logger.Level(logCfg.Level) diff --git a/log/options.go b/log/options.go index 831d9e4b4f97..f939fd486309 100644 --- a/log/options.go +++ b/log/options.go @@ -1,19 +1,27 @@ package log -import "github.com/rs/zerolog" +import ( + "time" -// defaultConfig has all the options disabled. + "github.com/rs/zerolog" +) + +// defaultConfig has all the options disabled, except Color and TimeFormat var defaultConfig = Config{ Level: zerolog.NoLevel, Filter: nil, OutputJSON: false, + Color: true, + TimeFormat: time.Kitchen, } -// LoggerConfig defines configuration for the logger. +// Config defines configuration for the logger. type Config struct { Level zerolog.Level Filter FilterFunc OutputJSON bool + Color bool + TimeFormat string } type Option func(*Config) @@ -40,3 +48,33 @@ func OutputJSONOption() Option { cfg.OutputJSON = true } } + +// ColorOption add option to enable/disable coloring +// of the logs when console writer is in use +func ColorOption(val bool) Option { + return func(cfg *Config) { + cfg.Color = val + } +} + +// TimeFormatOption configures timestamp format of the logger +// timestamps disabled if empty. +// it is responsibility of the caller to provider correct values +// Supported formats: +// - time.Layout +// - time.ANSIC +// - time.UnixDate +// - time.RubyDate +// - time.RFC822 +// - time.RFC822Z +// - time.RFC850 +// - time.RFC1123 +// - time.RFC1123Z +// - time.RFC3339 +// - time.RFC3339Nano +// - time.Kitchen +func TimeFormatOption(format string) Option { + return func(cfg *Config) { + cfg.TimeFormat = format + } +}