[#224] Refactor logger tag configuration
All checks were successful
/ DCO (pull_request) Successful in 29s
/ Vulncheck (pull_request) Successful in 48s
/ Builds (pull_request) Successful in 1m21s
/ OCI image (pull_request) Successful in 1m55s
/ Lint (pull_request) Successful in 1m54s
/ Tests (pull_request) Successful in 51s
/ Integration tests (pull_request) Successful in 7m12s
/ Builds (push) Successful in 1m10s
/ Vulncheck (push) Successful in 1m5s
/ Lint (push) Successful in 3m8s
/ Tests (push) Successful in 1m9s
/ Integration tests (push) Successful in 5m51s
/ OCI image (push) Successful in 1m3s
All checks were successful
/ DCO (pull_request) Successful in 29s
/ Vulncheck (pull_request) Successful in 48s
/ Builds (pull_request) Successful in 1m21s
/ OCI image (pull_request) Successful in 1m55s
/ Lint (pull_request) Successful in 1m54s
/ Tests (pull_request) Successful in 51s
/ Integration tests (pull_request) Successful in 7m12s
/ Builds (push) Successful in 1m10s
/ Vulncheck (push) Successful in 1m5s
/ Lint (push) Successful in 3m8s
/ Tests (push) Successful in 1m9s
/ Integration tests (push) Successful in 5m51s
/ OCI image (push) Successful in 1m3s
Signed-off-by: Pavel Pogodaev <p.pogodaev@yadro.com>
This commit is contained in:
parent
f0b86c8ba7
commit
cb72d11515
7 changed files with 59 additions and 28 deletions
|
@ -114,7 +114,8 @@ type (
|
|||
}
|
||||
|
||||
tagsConfig struct {
|
||||
tagLogs sync.Map
|
||||
tagLogs sync.Map
|
||||
defaultLvl zap.AtomicLevel
|
||||
}
|
||||
|
||||
logLevelConfig struct {
|
||||
|
@ -134,19 +135,34 @@ func newLogLevel(v *viper.Viper) zap.AtomicLevel {
|
|||
}
|
||||
|
||||
func newTagsConfig(v *viper.Viper, ll zapcore.Level) *tagsConfig {
|
||||
var t tagsConfig
|
||||
t := tagsConfig{defaultLvl: zap.NewAtomicLevelAt(ll)}
|
||||
if err := t.update(v, ll); err != nil {
|
||||
// panic here is analogue of the similar panic during common log level initialization.
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
return &t
|
||||
}
|
||||
|
||||
func newLogLevelConfig(lvl zap.AtomicLevel, tagsConfig *tagsConfig) *logLevelConfig {
|
||||
return &logLevelConfig{
|
||||
cfg := &logLevelConfig{
|
||||
logLevel: lvl,
|
||||
tagsConfig: tagsConfig,
|
||||
}
|
||||
|
||||
cfg.setMinLogLevel()
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
func (l *logLevelConfig) setMinLogLevel() {
|
||||
l.tagsConfig.tagLogs.Range(func(_, value any) bool {
|
||||
v := value.(zapcore.Level)
|
||||
if v < l.logLevel.Level() {
|
||||
l.logLevel.SetLevel(v)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func (l *logLevelConfig) update(cfg *viper.Viper, log *zap.Logger) {
|
||||
|
@ -159,17 +175,23 @@ 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.setMinLogLevel()
|
||||
}
|
||||
|
||||
func (t *tagsConfig) LevelEnabled(tag string, tgtLevel zapcore.Level) bool {
|
||||
lvl, ok := t.tagLogs.Load(tag)
|
||||
if !ok {
|
||||
return false
|
||||
return t.defaultLvl.Enabled(tgtLevel)
|
||||
}
|
||||
|
||||
return lvl.(zapcore.Level).Enabled(tgtLevel)
|
||||
}
|
||||
|
||||
func (t *tagsConfig) DefaultEnabled(lvl zapcore.Level) bool {
|
||||
return t.defaultLvl.Enabled(lvl)
|
||||
}
|
||||
|
||||
func (t *tagsConfig) update(cfg *viper.Viper, ll zapcore.Level) error {
|
||||
tags, err := fetchLogTagsConfig(cfg, ll)
|
||||
if err != nil {
|
||||
|
@ -194,6 +216,7 @@ func (t *tagsConfig) update(cfg *viper.Viper, ll zapcore.Level) error {
|
|||
for k, v := range tags {
|
||||
t.tagLogs.Store(k, v)
|
||||
}
|
||||
t.defaultLvl.SetLevel(ll)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue