From 4bf94da8c26fd73262b0da669ce80814a635beda Mon Sep 17 00:00:00 2001 From: Andrey Butusov Date: Tue, 5 Nov 2024 18:00:16 +0300 Subject: [PATCH] logger: add `encoding` config option Add a new config option for the logger encoding, which could be `console` or `json`. Update examples, doc and tests. Closes #2982. Signed-off-by: Andrey Butusov --- CHANGELOG.md | 1 + cmd/neofs-ir/defaults.go | 1 + cmd/neofs-ir/internal/validate/config.go | 3 ++- cmd/neofs-ir/main.go | 2 +- cmd/neofs-node/config.go | 6 ++++-- .../config/internal/validate/config.go | 3 ++- cmd/neofs-node/config/logger/config.go | 19 +++++++++++++++++++ cmd/neofs-node/config/logger/config_test.go | 9 +++++---- cmd/neofs-node/validate.go | 5 +++++ config/example/ir.env | 1 + config/example/ir.yaml | 1 + config/example/node.env | 1 + config/example/node.json | 3 ++- config/example/node.yaml | 1 + docs/storage-node-configuration.md | 8 +++++--- 15 files changed, 51 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1aef08c7f4..c88e3a09a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ attribute, which is used for container domain name in NNS contracts (#2954) - `neofs-cli control object revive` command (#2968) - `--disable-auto-gen-tag` flag for gendoc command (#2983) - Docs files for cli commands to the `docs/cli-commands` folder (#2983) +- `logger.encoding` config option (#2999) ### Fixed - Do not search for tombstones when handling their expiration, use local indexes instead (#2929) diff --git a/cmd/neofs-ir/defaults.go b/cmd/neofs-ir/defaults.go index 8517c850cf..3d6692f814 100644 --- a/cmd/neofs-ir/defaults.go +++ b/cmd/neofs-ir/defaults.go @@ -45,6 +45,7 @@ func newConfig(path string) (*viper.Viper, error) { func defaultConfiguration(cfg *viper.Viper) { cfg.SetDefault("logger.level", "info") + cfg.SetDefault("logger.encoding", "console") cfg.SetDefault("pprof.address", "localhost:6060") cfg.SetDefault("pprof.shutdown_timeout", "30s") diff --git a/cmd/neofs-ir/internal/validate/config.go b/cmd/neofs-ir/internal/validate/config.go index 8568b42af2..ae40d905bf 100644 --- a/cmd/neofs-ir/internal/validate/config.go +++ b/cmd/neofs-ir/internal/validate/config.go @@ -4,7 +4,8 @@ import "time" type validConfig struct { Logger struct { - Level string `mapstructure:"level"` + Level string `mapstructure:"level"` + Encoding string `mapstructure:"encoding"` } `mapstructure:"logger"` Wallet struct { diff --git a/cmd/neofs-ir/main.go b/cmd/neofs-ir/main.go index 8e3f293777..860ffa31bc 100644 --- a/cmd/neofs-ir/main.go +++ b/cmd/neofs-ir/main.go @@ -65,7 +65,7 @@ func main() { c := zap.NewProductionConfig() c.Level = logLevel - c.Encoding = "console" + c.Encoding = cfg.GetString("logger.encoding") if term.IsTerminal(int(os.Stdout.Fd())) { c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder } else { diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 76d762ad92..1942548b50 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -87,7 +87,8 @@ type applicationConfiguration struct { _read bool logger struct { - level string + level string + encoding string } engine struct { @@ -144,6 +145,7 @@ func (a *applicationConfiguration) readConfig(c *config.Config) error { // Logger a.logger.level = loggerconfig.Level(c) + a.logger.encoding = loggerconfig.Encoding(c) // Policer @@ -567,7 +569,7 @@ func initCfg(appCfg *config.Config) *cfg { logCfg := zap.NewProductionConfig() logCfg.Level = c.internals.logLevel - logCfg.Encoding = "console" + logCfg.Encoding = c.logger.encoding if term.IsTerminal(int(os.Stdout.Fd())) { logCfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder } else { diff --git a/cmd/neofs-node/config/internal/validate/config.go b/cmd/neofs-node/config/internal/validate/config.go index 7fe047cff9..5c6d8f609e 100644 --- a/cmd/neofs-node/config/internal/validate/config.go +++ b/cmd/neofs-node/config/internal/validate/config.go @@ -6,7 +6,8 @@ import ( type valideConfig struct { Logger struct { - Level string `mapstructure:"level"` + Level string `mapstructure:"level"` + Encoding string `mapstructure:"encoding"` } `mapstructure:"logger"` Pprof struct { diff --git a/cmd/neofs-node/config/logger/config.go b/cmd/neofs-node/config/logger/config.go index f994db0563..c4b69b357a 100644 --- a/cmd/neofs-node/config/logger/config.go +++ b/cmd/neofs-node/config/logger/config.go @@ -7,6 +7,9 @@ import ( const ( // LevelDefault is a default logger level. LevelDefault = "info" + + // EncodingDefault is a default logger encoding. + EncodingDefault = "console" ) // Level returns the value of "level" config parameter @@ -24,3 +27,19 @@ func Level(c *config.Config) string { return LevelDefault } + +// Encoding returns the value of "encoding" config parameter +// from "logger" section. +// +// Returns EncodingDefault if the value is not a non-empty string. +func Encoding(c *config.Config) string { + v := config.StringSafe( + c.Sub("logger"), + "encoding", + ) + if v != "" { + return v + } + + return EncodingDefault +} diff --git a/cmd/neofs-node/config/logger/config_test.go b/cmd/neofs-node/config/logger/config_test.go index faf2c79978..d6b807cd33 100644 --- a/cmd/neofs-node/config/logger/config_test.go +++ b/cmd/neofs-node/config/logger/config_test.go @@ -11,15 +11,16 @@ import ( func TestLoggerSection_Level(t *testing.T) { t.Run("defaults", func(t *testing.T) { - v := loggerconfig.Level(configtest.EmptyConfig()) - require.Equal(t, loggerconfig.LevelDefault, v) + emptyConfig := configtest.EmptyConfig() + require.Equal(t, loggerconfig.LevelDefault, loggerconfig.Level(emptyConfig)) + require.Equal(t, loggerconfig.EncodingDefault, loggerconfig.Encoding(emptyConfig)) }) const path = "../../../../config/example/node" var fileConfigTest = func(c *config.Config) { - v := loggerconfig.Level(c) - require.Equal(t, "debug", v) + require.Equal(t, "debug", loggerconfig.Level(c)) + require.Equal(t, "json", loggerconfig.Encoding(c)) } configtest.ForEachFileType(path, fileConfigTest) diff --git a/cmd/neofs-node/validate.go b/cmd/neofs-node/validate.go index 0f32123d63..25d9de7521 100644 --- a/cmd/neofs-node/validate.go +++ b/cmd/neofs-node/validate.go @@ -23,6 +23,11 @@ func validateConfig(c *config.Config) error { return fmt.Errorf("invalid logger level: %w", err) } + logEncoding := loggerconfig.Encoding(c) + if logEncoding != "console" && logEncoding != "json" { + return fmt.Errorf("invalid logger encoding: %s", logEncoding) + } + // shard configuration validation shardNum := 0 diff --git a/config/example/ir.env b/config/example/ir.env index 5ba111c28b..422a2dc05a 100644 --- a/config/example/ir.env +++ b/config/example/ir.env @@ -1,4 +1,5 @@ NEOFS_IR_LOGGER_LEVEL=info +NEOFS_IR_LOGGER_ENCODING=console NEOFS_IR_WALLET_PATH=/path/to/wallet.json NEOFS_IR_WALLET_ADDRESS=NUHtW3eM6a4mmFCgyyr4rj4wygsTKB88XX diff --git a/config/example/ir.yaml b/config/example/ir.yaml index 670ad26670..b729859203 100644 --- a/config/example/ir.yaml +++ b/config/example/ir.yaml @@ -2,6 +2,7 @@ logger: level: info # Logger level: one of "debug", "info" (default), "warn", "error", "dpanic", "panic", "fatal" + encoding: console # Logger encoding: one of "console" (default) or "json" wallet: path: /path/to/wallet.json # Path to NEP-6 NEO wallet file diff --git a/config/example/node.env b/config/example/node.env index 269854897d..f657b07226 100644 --- a/config/example/node.env +++ b/config/example/node.env @@ -1,4 +1,5 @@ NEOFS_LOGGER_LEVEL=debug +NEOFS_LOGGER_ENCODING=json NEOFS_PPROF_ENABLED=true NEOFS_PPROF_ADDRESS=localhost:6060 diff --git a/config/example/node.json b/config/example/node.json index 331485fea7..92054143d7 100644 --- a/config/example/node.json +++ b/config/example/node.json @@ -1,6 +1,7 @@ { "logger": { - "level": "debug" + "level": "debug", + "encoding": "json" }, "pprof": { "enabled": true, diff --git a/config/example/node.yaml b/config/example/node.yaml index 85004e96ff..524e77b7eb 100644 --- a/config/example/node.yaml +++ b/config/example/node.yaml @@ -1,5 +1,6 @@ logger: level: debug # logger level: one of "debug", "info" (default), "warn", "error", "dpanic", "panic", "fatal" + encoding: json # logger encoding: one of "console" (default) or "json" pprof: enabled: true diff --git a/docs/storage-node-configuration.md b/docs/storage-node-configuration.md index e0a11107f2..9dde14f3af 100644 --- a/docs/storage-node-configuration.md +++ b/docs/storage-node-configuration.md @@ -101,11 +101,13 @@ Contains logger parameters. ```yaml logger: level: info + encoding: console ``` -| Parameter | Type | Default value | Description | -|-----------|----------|---------------|---------------------------------------------------------------------------------------------------| -| `level` | `string` | `info` | Logging level.
Possible values: `debug`, `info`, `warn`, `error`, `dpanic`, `panic`, `fatal` | +| Parameter | Type | Default value | Description | +|-------------|------------|---------------|---------------------------------------------------------------------------------------------------| +| `level` | `string` | `info` | Logging level.
Possible values: `debug`, `info`, `warn`, `error`, `dpanic`, `panic`, `fatal` | +| `encoding` | `string` | `console` | Logging encoding.
Possible values: `console`, `json` | # `contracts` section Contains override values for NeoFS side-chain contract hashes. Most of the time contract