From 2a970b0125dff17833a01d8c447719d25f06989b Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 11 May 2021 11:25:54 +0300 Subject: [PATCH] [#493] logger: Do not set global fields in constructor Remove setting of `app_name` and `app_version` fields in `NewLogger` constructor. Set these fields in Node and IR application in already constructed log and remove them from viper . Signed-off-by: Leonard Lyubich --- cmd/neofs-ir/defaults.go | 3 --- cmd/neofs-ir/main.go | 5 +++++ cmd/neofs-node/config.go | 8 +++++--- pkg/util/logger/logger.go | 12 +----------- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/cmd/neofs-ir/defaults.go b/cmd/neofs-ir/defaults.go index c245654b..3c1c247b 100644 --- a/cmd/neofs-ir/defaults.go +++ b/cmd/neofs-ir/defaults.go @@ -18,9 +18,6 @@ func newConfig(path string) (*viper.Viper, error) { v.AutomaticEnv() v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - v.SetDefault("app.name", misc.InnerRingName) - v.SetDefault("app.version", misc.Version) - defaultConfiguration(v) if path != "" { diff --git a/cmd/neofs-ir/main.go b/cmd/neofs-ir/main.go index 89ad6b49..7457da52 100644 --- a/cmd/neofs-ir/main.go +++ b/cmd/neofs-ir/main.go @@ -47,6 +47,11 @@ func main() { log, err := logger.NewLogger(cfg) exitErr(err) + log = log.With( + zap.String("app_name", misc.InnerRingName), + zap.String("app_version", misc.Version), + ) + ctx := grace.NewGracefulContext(log) intErr := make(chan error) // internal inner ring errors diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index af244d3e..e276299e 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -355,6 +355,11 @@ func initCfg(path string) *cfg { log, err := logger.NewLogger(viperCfg) fatalOnErr(err) + log = log.With( + zap.String("app_name", misc.NodeName), + zap.String("app_version", misc.Version), + ) + netAddr, err := network.AddressFromString(viperCfg.GetString(cfgBootstrapAddress)) fatalOnErr(err) @@ -442,9 +447,6 @@ func initViper(path string) *viper.Viper { v.AutomaticEnv() v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - v.SetDefault("app.name", misc.NodeName) - v.SetDefault("app.version", misc.Version) - defaultConfiguration(v) if path != "" { diff --git a/pkg/util/logger/logger.go b/pkg/util/logger/logger.go index 7f0da8e9..179575d9 100644 --- a/pkg/util/logger/logger.go +++ b/pkg/util/logger/logger.go @@ -27,20 +27,10 @@ func NewLogger(v *viper.Viper) (*Logger, error) { c.Encoding = "console" c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder - l, err := c.Build( + return c.Build( // record a stack trace for all messages at or above fatal level zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)), ) - if err != nil { - return nil, err - } - - name := v.GetString("app.name") - version := v.GetString("app.version") - - return l.With( - zap.String("app_name", name), - zap.String("app_version", version)), nil } func safeLevel(lvl string) zap.AtomicLevel {