From db2e43387bb8e382ab36304e1860406713b443bd Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 11 May 2021 11:54:59 +0300 Subject: [PATCH] [#493] logger: Simplify Logger's parameters Replace `viper.Viper` parameter with `Prm` structure. Currently only logging level can be parameterized through string setter. Signed-off-by: Leonard Lyubich --- cmd/neofs-ir/main.go | 9 +++++++- cmd/neofs-node/config.go | 9 +++++++- go.sum | Bin 59738 -> 60131 bytes pkg/util/logger/logger.go | 46 +++++++++++++++++++------------------- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/cmd/neofs-ir/main.go b/cmd/neofs-ir/main.go index 7457da523..d6ae0d27d 100644 --- a/cmd/neofs-ir/main.go +++ b/cmd/neofs-ir/main.go @@ -44,7 +44,14 @@ func main() { cfg, err := newConfig(*configFile) exitErr(err) - log, err := logger.NewLogger(cfg) + var logPrm logger.Prm + + err = logPrm.SetLevelString( + cfg.GetString("logger.level"), + ) + exitErr(err) + + log, err := logger.NewLogger(logPrm) exitErr(err) log = log.With( diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index e276299ec..016b0671b 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -352,7 +352,14 @@ func initCfg(path string) *cfg { viperCfg.GetString(cfgReputationContract)) fatalOnErr(err) - log, err := logger.NewLogger(viperCfg) + var logPrm logger.Prm + + err = logPrm.SetLevelString( + viperCfg.GetString(cfgLogLevel), + ) + fatalOnErr(err) + + log, err := logger.NewLogger(logPrm) fatalOnErr(err) log = log.With( diff --git a/go.sum b/go.sum index f601bfccc0a3072dbf4bdbc95cc151c7390dcc20..79877c9a4942dc6b15b0a8daf0b989c76d9f4cc6 100644 GIT binary patch delta 241 zcmcb0iTUwW<_-J#^c6A;t-K-&%srC=vl7$towQ4w9F5a`D?K7TiVFNo{7PLiz4c3- zOiLpw3Qfy>C+qV{OqS*o-n^WTg%PaTqu9bI%EYkPEI7r-(Am`|TstDsB{J9F!z3fy z$T8I_Z2VOSIq enqT4XU6n3i9ay!p+&JT3rVfKUJc delta 28 mcmV+%0OSAT)dSkn1F*jilQ9qMvy2d`bF+wqzFV`L;71DAFb-<~ diff --git a/pkg/util/logger/logger.go b/pkg/util/logger/logger.go index 179575d99..462b38e2a 100644 --- a/pkg/util/logger/logger.go +++ b/pkg/util/logger/logger.go @@ -2,9 +2,7 @@ package logger import ( "errors" - "strings" - "github.com/spf13/viper" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -16,36 +14,38 @@ import ( // go.uber.org/zap.Logger. type Logger = zap.Logger +// Prm groups Logger's parameters. +type Prm struct { + level zapcore.Level +} + // ErrNilLogger is returned by functions that // expect a non-nil Logger, but received nil. var ErrNilLogger = errors.New("logger is nil") -// NewLogger is a logger's constructor. -func NewLogger(v *viper.Viper) (*Logger, error) { +// SetLevelString sets minimum logging level. +// +// Returns error of s is not a string representation of zap.Level +// value (see zapcore.Level docs). +func (p *Prm) SetLevelString(s string) error { + return p.level.UnmarshalText([]byte(s)) +} + +// NewLogger constructs a new zap logger instance. +// +// Logger is built from production logging configuration with: +// * parameterized level; +// * console encoding; +// * ISO8601 time encoding. +// +// Logger records a stack trace for all messages at or above fatal level. +func NewLogger(prm Prm) (*Logger, error) { c := zap.NewProductionConfig() - c.Level = safeLevel(v.GetString("logger.level")) + c.Level = zap.NewAtomicLevelAt(prm.level) c.Encoding = "console" c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder return c.Build( - // record a stack trace for all messages at or above fatal level zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)), ) } - -func safeLevel(lvl string) zap.AtomicLevel { - switch strings.ToLower(lvl) { - case "debug": - return zap.NewAtomicLevelAt(zap.DebugLevel) - case "warn": - return zap.NewAtomicLevelAt(zap.WarnLevel) - case "error": - return zap.NewAtomicLevelAt(zap.ErrorLevel) - case "fatal": - return zap.NewAtomicLevelAt(zap.FatalLevel) - case "panic": - return zap.NewAtomicLevelAt(zap.PanicLevel) - default: - return zap.NewAtomicLevelAt(zap.InfoLevel) - } -}