From 06db52e53de87c297e1e8f8d5d11f1f4dcf58245 Mon Sep 17 00:00:00 2001
From: Pavel Pogodaev
Date: Fri, 21 Mar 2025 13:38:43 +0300
Subject: [PATCH] [#224] Refactor logger tag configuration
Signed-off-by: Pavel Pogodaev
---
cmd/http-gw/app.go | 24 +++++++++++++++++++++++-
cmd/http-gw/logger.go | 14 ++++++++------
cmd/http-gw/settings.go | 13 +++++++++----
config/config.env | 5 +++--
config/config.yaml | 3 +--
docs/gate-configuration.md | 15 +++++++--------
6 files changed, 51 insertions(+), 23 deletions(-)
diff --git a/cmd/http-gw/app.go b/cmd/http-gw/app.go
index c75f9d8..77e657b 100644
--- a/cmd/http-gw/app.go
+++ b/cmd/http-gw/app.go
@@ -119,10 +119,15 @@ type (
logLevelConfig struct {
logLevel zap.AtomicLevel
+ maxLevel zap.AtomicLevel
tagsConfig *tagsConfig
}
)
+func (l *logLevelConfig) MaxLevel() zap.AtomicLevel {
+ return l.maxLevel
+}
+
func newLogLevel(v *viper.Viper) zap.AtomicLevel {
ll, err := getLogLevel(v)
if err != nil {
@@ -143,12 +148,27 @@ func newTagsConfig(v *viper.Viper, ll zapcore.Level) *tagsConfig {
}
func newLogLevelConfig(lvl zap.AtomicLevel, tagsConfig *tagsConfig) *logLevelConfig {
+ maxLvl := maxLogLevel(lvl, tagsConfig)
return &logLevelConfig{
- logLevel: lvl,
+ logLevel: maxLvl,
tagsConfig: tagsConfig,
+ maxLevel: maxLvl,
}
}
+func maxLogLevel(lvl zap.AtomicLevel, config *tagsConfig) zap.AtomicLevel {
+ maxLvl := lvl
+ config.tagLogs.Range(func(_, value any) bool {
+ v := value.(zapcore.Level)
+ if v > maxLvl.Level() {
+ maxLvl = zap.NewAtomicLevelAt(v)
+ }
+ return true
+ })
+
+ return maxLvl
+}
+
func (l *logLevelConfig) update(cfg *viper.Viper, log *zap.Logger) {
if lvl, err := getLogLevel(cfg); err != nil {
log.Warn(logs.LogLevelWontBeUpdated, zap.Error(err), logs.TagField(logs.TagApp))
@@ -159,6 +179,8 @@ func (l *logLevelConfig) update(cfg *viper.Viper, log *zap.Logger) {
if err := l.tagsConfig.update(cfg, l.logLevel.Level()); err != nil {
log.Warn(logs.TagsLogConfigWontBeUpdated, zap.Error(err), logs.TagField(logs.TagApp))
}
+
+ l.maxLevel = maxLogLevel(l.logLevel, l.tagsConfig)
}
func (t *tagsConfig) LevelEnabled(tag string, tgtLevel zapcore.Level) bool {
diff --git a/cmd/http-gw/logger.go b/cmd/http-gw/logger.go
index 91105f7..e5821ad 100644
--- a/cmd/http-gw/logger.go
+++ b/cmd/http-gw/logger.go
@@ -36,6 +36,7 @@ var _ zapcore.Core = (*zapCoreTagFilterWrapper)(nil)
type zapCoreTagFilterWrapper struct {
core zapcore.Core
settings TagFilterSettings
+ maxLvl MaxLevelSetting
extra []zap.Field
}
@@ -43,6 +44,10 @@ type TagFilterSettings interface {
LevelEnabled(tag string, lvl zapcore.Level) bool
}
+type MaxLevelSetting interface {
+ MaxLevel() zap.AtomicLevel
+}
+
func (c *zapCoreTagFilterWrapper) Enabled(level zapcore.Level) bool {
return c.core.Enabled(level)
}
@@ -51,6 +56,7 @@ func (c *zapCoreTagFilterWrapper) With(fields []zapcore.Field) zapcore.Core {
return &zapCoreTagFilterWrapper{
core: c.core.With(fields),
settings: c.settings,
+ maxLvl: c.maxLvl,
extra: append(c.extra, fields...),
}
}
@@ -73,14 +79,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.maxLvl.MaxLevel().Enabled(entry.Level)
}
func (c *zapCoreTagFilterWrapper) Sync() error {
diff --git a/cmd/http-gw/settings.go b/cmd/http-gw/settings.go
index 132c627..982b401 100644
--- a/cmd/http-gw/settings.go
+++ b/cmd/http-gw/settings.go
@@ -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) {
diff --git a/config/config.env b/config/config.env
index 0ff2dec..72492d8 100644
--- a/config/config.env
+++ b/config/config.env
@@ -20,8 +20,9 @@ HTTP_GW_LOGGER_SAMPLING_ENABLED=false
HTTP_GW_LOGGER_SAMPLING_INITIAL=100
HTTP_GW_LOGGER_SAMPLING_THEREAFTER=100
HTTP_GW_LOGGER_SAMPLING_INTERVAL=1s
-HTTP_GW_LOGGER_TAGS_0_NAME=app
-HTTP_GW_LOGGER_TAGS_1_NAME=datapath
+HTTP_GW_LOGGER_TAGS_0_NAMES=app,datapath
+HTTP_GW_LOGGER_TAGS_0_LEVEL=level
+HTTP_GW_LOGGER_TAGS_1_NAME=external_storage_tree
HTTP_GW_SERVER_0_ADDRESS=0.0.0.0:443
HTTP_GW_SERVER_0_TLS_ENABLED=false
diff --git a/config/config.yaml b/config/config.yaml
index 05bba2e..ccd025e 100644
--- a/config/config.yaml
+++ b/config/config.yaml
@@ -30,8 +30,7 @@ logger:
thereafter: 100
interval: 1s
tags:
- - name: app
- - name: datapath
+ - names: app,datapath
level: debug
server:
diff --git a/docs/gate-configuration.md b/docs/gate-configuration.md
index 628d3c7..1dec574 100644
--- a/docs/gate-configuration.md
+++ b/docs/gate-configuration.md
@@ -176,10 +176,9 @@ logger:
thereafter: 100
interval: 1s
tags:
- - name: "app"
+ - names: "app,datapath"
level: info
- - name: "datapath"
- - name: "external_storage_tree"
+ - names: "external_storage_tree"
```
| Parameter | Type | SIGHUP reload | Default value | Description |
@@ -199,14 +198,14 @@ parameter. Available tags:
```yaml
tags:
- - name: "app"
+ - names: "app,datapath"
level: info
```
-| Parameter | Type | SIGHUP reload | Default value | Description |
-|-----------------------|------------|---------------|---------------------------|-------------------------------------------------------------------------------------------------------|
-| `name` | `string` | yes | | Tag name. Possible values see below in `Tag values` section. |
-| `level` | `string` | yes | Value from `logger.level` | Logging level for specific tag. Possible values: `debug`, `info`, `warn`, `dpanic`, `panic`, `fatal`. |
+| Parameter | Type | SIGHUP reload | Default value | Description |
+|-----------|------------|---------------|---------------------------|-------------------------------------------------------------------------------------------------------|
+| `names` | `[]string` | yes | | Tag names separated by `,`. Possible values see below in `Tag values` section. |
+| `level` | `string` | yes | Value from `logger.level` | Logging level for specific tag. Possible values: `debug`, `info`, `warn`, `dpanic`, `panic`, `fatal`. |
### Tag values