[#224] Refactor logger tag configuration
All checks were successful
/ DCO (pull_request) Successful in 27s
/ Vulncheck (pull_request) Successful in 59s
/ Builds (pull_request) Successful in 1m33s
/ OCI image (pull_request) Successful in 1m34s
/ Lint (pull_request) Successful in 2m36s
/ Tests (pull_request) Successful in 1m5s
/ Integration tests (pull_request) Successful in 6m28s

Signed-off-by: Pavel Pogodaev <p.pogodaev@yadro.com>
This commit is contained in:
Pavel Pogodaev 2025-03-21 13:38:43 +03:00
parent f0b86c8ba7
commit c9d2ad947c
7 changed files with 41 additions and 26 deletions

View file

@ -144,21 +144,35 @@ func newTagsConfig(v *viper.Viper, ll zapcore.Level) *tagsConfig {
func newLogLevelConfig(lvl zap.AtomicLevel, tagsConfig *tagsConfig) *logLevelConfig {
return &logLevelConfig{
logLevel: lvl,
logLevel: minLogLevel(lvl, tagsConfig),
tagsConfig: tagsConfig,
}
}
func minLogLevel(lvl zap.AtomicLevel, config *tagsConfig) zap.AtomicLevel {
minLvl := lvl
config.tagLogs.Range(func(_, value any) bool {
v := value.(zapcore.Level)
if v < minLvl.Level() {
minLvl = zap.NewAtomicLevelAt(v)
}
return true
})
return minLvl
}
func (l *logLevelConfig) update(cfg *viper.Viper, log *zap.Logger) {
if lvl, err := getLogLevel(cfg); err != nil {
lvl, err := getLogLevel(cfg)
if err != nil {
log.Warn(logs.LogLevelWontBeUpdated, zap.Error(err), logs.TagField(logs.TagApp))
} else {
l.logLevel.SetLevel(lvl)
}
if err := l.tagsConfig.update(cfg, l.logLevel.Level()); err != nil {
log.Warn(logs.TagsLogConfigWontBeUpdated, zap.Error(err), logs.TagField(logs.TagApp))
}
l.logLevel.SetLevel(minLogLevel(zap.NewAtomicLevelAt(lvl), l.tagsConfig).Level())
}
func (t *tagsConfig) LevelEnabled(tag string, tgtLevel zapcore.Level) bool {

View file

@ -73,14 +73,10 @@ func (c *zapCoreTagFilterWrapper) Write(entry zapcore.Entry, fields []zapcore.Fi
func (c *zapCoreTagFilterWrapper) shouldSkip(entry zapcore.Entry, fields []zap.Field) bool {
for _, field := range fields {
if field.Key == logs.TagFieldName && field.Type == zapcore.StringType {
if !c.settings.LevelEnabled(field.String, entry.Level) {
return true
}
break
return !c.settings.LevelEnabled(field.String, entry.Level)
}
}
return false
return !c.core.Enabled(entry.Level)
}
func (c *zapCoreTagFilterWrapper) Sync() error {

View file

@ -113,7 +113,7 @@ const (
cfgLoggerTags = "logger.tags"
cfgLoggerTagsPrefixTmpl = cfgLoggerTags + ".%d."
cfgLoggerTagsNameTmpl = cfgLoggerTagsPrefixTmpl + "name"
cfgLoggerTagsNameTmpl = cfgLoggerTagsPrefixTmpl + "names"
cfgLoggerTagsLevelTmpl = cfgLoggerTagsPrefixTmpl + "level"
// Wallet.
@ -516,8 +516,8 @@ func fetchLogTagsConfig(v *viper.Viper, defaultLvl zapcore.Level) (map[string]za
res := make(map[string]zapcore.Level)
for i := 0; ; i++ {
name := v.GetString(fmt.Sprintf(cfgLoggerTagsNameTmpl, i))
if name == "" {
tagNames := v.GetString(fmt.Sprintf(cfgLoggerTagsNameTmpl, i))
if tagNames == "" {
break
}
@ -529,7 +529,12 @@ func fetchLogTagsConfig(v *viper.Viper, defaultLvl zapcore.Level) (map[string]za
}
}
res[name] = lvl
for _, tagName := range strings.Split(tagNames, ",") {
tagName = strings.TrimSpace(tagName)
if len(tagName) != 0 {
res[tagName] = lvl
}
}
}
if len(res) == 0 && !v.IsSet(cfgLoggerTags) {