Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(log): extend logger options #15956

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
troian marked this conversation as resolved.
Show resolved Hide resolved
Color: true,
TimeFormat: time.Kitchen,
troian marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
}
}