Skip to content

Commit

Permalink
logger: add encoding config option
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
End-rey committed Nov 5, 2024
1 parent 2e583e5 commit 4bf94da
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/neofs-ir/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion cmd/neofs-ir/internal/validate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-ir/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func main() {

c := zap.NewProductionConfig()
c.Level = logLevel
c.Encoding = "console"
c.Encoding = cfg.GetString("logger.encoding")

Check warning on line 68 in cmd/neofs-ir/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-ir/main.go#L68

Added line #L68 was not covered by tests
if term.IsTerminal(int(os.Stdout.Fd())) {
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
} else {
Expand Down
6 changes: 4 additions & 2 deletions cmd/neofs-node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ type applicationConfiguration struct {
_read bool

logger struct {
level string
level string
encoding string
}

engine struct {
Expand Down Expand Up @@ -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)

Check warning on line 148 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L148

Added line #L148 was not covered by tests

// Policer

Expand Down Expand Up @@ -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

Check warning on line 572 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L572

Added line #L572 was not covered by tests
if term.IsTerminal(int(os.Stdout.Fd())) {
logCfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
} else {
Expand Down
3 changes: 2 additions & 1 deletion cmd/neofs-node/config/internal/validate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions cmd/neofs-node/config/logger/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
9 changes: 5 additions & 4 deletions cmd/neofs-node/config/logger/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions cmd/neofs-node/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Check warning on line 29 in cmd/neofs-node/validate.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/validate.go#L28-L29

Added lines #L28 - L29 were not covered by tests

// shard configuration validation

shardNum := 0
Expand Down
1 change: 1 addition & 0 deletions config/example/ir.env
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions config/example/ir.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/example/node.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NEOFS_LOGGER_LEVEL=debug
NEOFS_LOGGER_ENCODING=json

NEOFS_PPROF_ENABLED=true
NEOFS_PPROF_ADDRESS=localhost:6060
Expand Down
3 changes: 2 additions & 1 deletion config/example/node.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"logger": {
"level": "debug"
"level": "debug",
"encoding": "json"
},
"pprof": {
"enabled": true,
Expand Down
1 change: 1 addition & 0 deletions config/example/node.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 5 additions & 3 deletions docs/storage-node-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ Contains logger parameters.
```yaml
logger:
level: info
encoding: console
```

| Parameter | Type | Default value | Description |
|-----------|----------|---------------|---------------------------------------------------------------------------------------------------|
| `level` | `string` | `info` | Logging level.<br/>Possible values: `debug`, `info`, `warn`, `error`, `dpanic`, `panic`, `fatal` |
| Parameter | Type | Default value | Description |
|-------------|------------|---------------|---------------------------------------------------------------------------------------------------|
| `level` | `string` | `info` | Logging level.<br/>Possible values: `debug`, `info`, `warn`, `error`, `dpanic`, `panic`, `fatal` |
| `encoding` | `string` | `console` | Logging encoding.<br/>Possible values: `console`, `json` |

# `contracts` section
Contains override values for NeoFS side-chain contract hashes. Most of the time contract
Expand Down

0 comments on commit 4bf94da

Please sign in to comment.