feat(log): extend logger options (#15956)
Signed-off-by: Artur Troian <troian.ap@gmail.com>
This commit is contained in:
parent
851e9e873b
commit
be5e3aa52b
@ -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.
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user