From 883264db1bdfd8aa89af715a09cbd1f7b835b1b7 Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Sun, 30 Jul 2023 16:52:51 +0800 Subject: [PATCH] perf(log): parse log level in filterFunc (#17194) --- log/CHANGELOG.md | 4 ++++ log/level.go | 16 ++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/log/CHANGELOG.md b/log/CHANGELOG.md index 825178f18fcf..80a98e2fd61c 100644 --- a/log/CHANGELOG.md +++ b/log/CHANGELOG.md @@ -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). diff --git a/log/level.go b/log/level.go index 9a4f094f2c9f..d8523f136a6c 100644 --- a/log/level.go +++ b/log/level.go @@ -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, ":") @@ -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 } @@ -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 }