diff --git a/log/CHANGELOG.md b/log/CHANGELOG.md index f393ecc080..08768555e7 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 da1552ef6a..46ae75b619 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 831d9e4b4f..f939fd4863 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 + } +}