Skip to content

Commit

Permalink
feat(log): extend logger options (#15956)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <troian.ap@gmail.com>
  • Loading branch information
troian committed Apr 27, 2023
1 parent 851e9e8 commit be5e3aa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
4 changes: 4 additions & 0 deletions log/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 10 additions & 4 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
44 changes: 41 additions & 3 deletions log/options.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
}
}

0 comments on commit be5e3aa

Please sign in to comment.