Skip to content

Commit

Permalink
perf(log): parse log level in filterFunc (#17194)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jul 30, 2023
1 parent 0b5e7ed commit 883264d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
4 changes: 4 additions & 0 deletions log/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

* [#15956](https://github.com/cosmos/cosmos-sdk/pull/15956) Introduce an option for enabling error stack trace.

### Improvements

* [#17194](https://github.com/cosmos/cosmos-sdk/pull/17194) Avoid repeating parse log level in filterFunc.

## [v1.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/log/v1.1.0) - 2023-04-27

* [#15956](https://github.com/cosmos/cosmos-sdk/pull/15956) Introduce options to configure logger (enable/disable colored output, customize log timestamps).
Expand Down
16 changes: 6 additions & 10 deletions log/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func ParseLogLevel(levelStr string) (FilterFunc, error) {
}

// parse and validate the levels
filterMap := make(map[string]string)
filterMap := make(map[string]zerolog.Level)
list := strings.Split(l, ",")
for _, item := range list {
moduleAndLevel := strings.Split(item, ":")
Expand All @@ -50,18 +50,19 @@ func ParseLogLevel(levelStr string) (FilterFunc, error) {
return nil, fmt.Errorf("duplicate module %s in log level list %s", module, list)
}

if _, err := zerolog.ParseLevel(level); err != nil {
zllevel, err := zerolog.ParseLevel(level)
if err != nil {
return nil, fmt.Errorf("invalid log level %s in log level list %s", level, list)
}

filterMap[module] = level
filterMap[module] = zllevel
}

filterFunc := func(key, lvl string) bool {
level, ok := filterMap[key]
zllevel, ok := filterMap[key]
if !ok { // no level filter for this key
// check if there is a default level filter
level, ok = filterMap[defaultLogLevelKey]
zllevel, ok = filterMap[defaultLogLevelKey]
if !ok {
return false
}
Expand All @@ -72,11 +73,6 @@ func ParseLogLevel(levelStr string) (FilterFunc, error) {
panic(err)
}

zllevel, err := zerolog.ParseLevel(level)
if err != nil {
panic(err)
}

return zllvl < zllevel
}

Expand Down

0 comments on commit 883264d

Please sign in to comment.