[#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"
)
// LoggerSection represents config section
// for logging component.
type LoggerSection config.Config
// config defaults
const (
// LevelDefault is a default logger level
LevelDefault = "info"
)
// Init initializes LoggerSection from
// "logger" subsection of config.
func Init(root *config.Config) *LoggerSection {
return (*LoggerSection)(root.Sub("logger"))
}
// Level returns configuration value with name "level".
// Level returns value of "level" config parameter
// from "logger" section.
//
// Returns LevelDefault if value is not a non-empty string.
func (x *LoggerSection) Level() string {
v := config.StringSafe((*config.Config)(x), "level")
func Level(c *config.Config) string {
v := config.StringSafe(
c.Sub("logger"),
"level",
)
if v != "" {
return v
}

View file

@ -12,20 +12,13 @@ import (
)
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) {
cfg := loggerconfig.Init(c)
checkLevel(cfg, "debug")
v := loggerconfig.Level(c)
require.Equal(t, "debug", v)
})
empty := loggerconfig.Init(configtest.EmptyConfig())
checkLevel(empty, loggerconfig.LevelDefault)
v := loggerconfig.Level(configtest.EmptyConfig())
require.Equal(t, loggerconfig.LevelDefault, v)
t.Run("ENV", func(t *testing.T) {
// TODO: read from file
@ -35,8 +28,7 @@ func TestLoggerSection_Level(t *testing.T) {
)
require.NoError(t, err)
empty = loggerconfig.Init(configtest.EmptyConfig())
checkLevel(empty, "debug")
v := loggerconfig.Level(configtest.EmptyConfig())
require.Equal(t, "debug", v)
})
}