[#493] node/config/logger: Simplify approach to read the level

Dedicated type `LoggerSection` turned out to be redundant since it doesn't
do a hidden logic and just uses `config.Config` API.

Remove `LoggerSection` type and implement `Level` which do the same.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-21 21:03:47 +03:00 committed by Leonard Lyubich
parent be6898a51d
commit 3b797d7957
2 changed files with 13 additions and 27 deletions

View file

@ -4,27 +4,21 @@ import (
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
) )
// LoggerSection represents config section
// for logging component.
type LoggerSection config.Config
// config defaults // config defaults
const ( const (
// LevelDefault is a default logger level // LevelDefault is a default logger level
LevelDefault = "info" LevelDefault = "info"
) )
// Init initializes LoggerSection from // Level returns value of "level" config parameter
// "logger" subsection of config. // from "logger" section.
func Init(root *config.Config) *LoggerSection {
return (*LoggerSection)(root.Sub("logger"))
}
// Level returns configuration value with name "level".
// //
// Returns LevelDefault if value is not a non-empty string. // Returns LevelDefault if value is not a non-empty string.
func (x *LoggerSection) Level() string { func Level(c *config.Config) string {
v := config.StringSafe((*config.Config)(x), "level") v := config.StringSafe(
c.Sub("logger"),
"level",
)
if v != "" { if v != "" {
return v return v
} }

View file

@ -12,20 +12,13 @@ import (
) )
func TestLoggerSection_Level(t *testing.T) { func TestLoggerSection_Level(t *testing.T) {
checkLevel := func(c *loggerconfig.LoggerSection, expected string) {
lvl := c.Level()
require.Equal(t, expected, lvl)
}
configtest.ForEachFileType("../../../../config/example/node", func(c *config.Config) { configtest.ForEachFileType("../../../../config/example/node", func(c *config.Config) {
cfg := loggerconfig.Init(c) v := loggerconfig.Level(c)
require.Equal(t, "debug", v)
checkLevel(cfg, "debug")
}) })
empty := loggerconfig.Init(configtest.EmptyConfig()) v := loggerconfig.Level(configtest.EmptyConfig())
require.Equal(t, loggerconfig.LevelDefault, v)
checkLevel(empty, loggerconfig.LevelDefault)
t.Run("ENV", func(t *testing.T) { t.Run("ENV", func(t *testing.T) {
// TODO: read from file // TODO: read from file
@ -35,8 +28,7 @@ func TestLoggerSection_Level(t *testing.T) {
) )
require.NoError(t, err) require.NoError(t, err)
empty = loggerconfig.Init(configtest.EmptyConfig()) v := loggerconfig.Level(configtest.EmptyConfig())
require.Equal(t, "debug", v)
checkLevel(empty, "debug")
}) })
} }