Skip to content

Commit de89b7a

Browse files
authored
logger: add timestamp config option (#3105)
Closes #3067.
2 parents 1f6d1d8 + f5f639e commit de89b7a

File tree

13 files changed

+39
-13
lines changed

13 files changed

+39
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Changelog for NeoFS Node
88
- First split-object part into the CLI output (#3064)
99
- `neofs-cli control notary` with `list`, `request` and `sign` commands (#3059)
1010
- IR `fschain.consensus.keep_only_latest_state` and `fschain.consensus.remove_untraceable_blocks` config options (#3093)
11+
- `logger.timestamp` config option (#3105)
1112

1213
### Fixed
1314
- `neofs-cli object delete` command output (#3056)

cmd/neofs-ir/internal/validate/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import "time"
44

55
type validConfig struct {
66
Logger struct {
7-
Level string `mapstructure:"level"`
8-
Encoding string `mapstructure:"encoding"`
7+
Level string `mapstructure:"level"`
8+
Encoding string `mapstructure:"encoding"`
9+
Timestamp bool `mapstructure:"timestamp"`
910
} `mapstructure:"logger"`
1011

1112
Wallet struct {

cmd/neofs-ir/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func main() {
6767
c.Level = logLevel
6868
c.Encoding = cfg.GetString("logger.encoding")
6969
c.Sampling = nil
70-
if term.IsTerminal(int(os.Stdout.Fd())) {
70+
if (term.IsTerminal(int(os.Stdout.Fd())) && !cfg.IsSet("logger.timestamp")) || cfg.GetBool("logger.timestamp") {
7171
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
7272
} else {
7373
c.EncoderConfig.EncodeTime = func(_ time.Time, _ zapcore.PrimitiveArrayEncoder) {}

cmd/neofs-node/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ type applicationConfiguration struct {
8787
_read bool
8888

8989
logger struct {
90-
level string
91-
encoding string
90+
level string
91+
encoding string
92+
timestamp bool
9293
}
9394

9495
engine struct {
@@ -147,6 +148,7 @@ func (a *applicationConfiguration) readConfig(c *config.Config) error {
147148

148149
a.logger.level = loggerconfig.Level(c)
149150
a.logger.encoding = loggerconfig.Encoding(c)
151+
a.logger.timestamp = loggerconfig.Timestamp(c)
150152

151153
// Policer
152154

@@ -562,7 +564,7 @@ func initCfg(appCfg *config.Config) *cfg {
562564
logCfg.Level = c.internals.logLevel
563565
logCfg.Encoding = c.logger.encoding
564566
logCfg.Sampling = nil
565-
if term.IsTerminal(int(os.Stdout.Fd())) {
567+
if (term.IsTerminal(int(os.Stdout.Fd())) && c.cfgReader.Value("logger.timestamp") == nil) || c.logger.timestamp {
566568
logCfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
567569
} else {
568570
logCfg.EncoderConfig.EncodeTime = func(_ time.Time, _ zapcore.PrimitiveArrayEncoder) {}

cmd/neofs-node/config/internal/validate/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66

77
type valideConfig struct {
88
Logger struct {
9-
Level string `mapstructure:"level"`
10-
Encoding string `mapstructure:"encoding"`
9+
Level string `mapstructure:"level"`
10+
Encoding string `mapstructure:"encoding"`
11+
Timestamp bool `mapstructure:"timestamp"`
1112
} `mapstructure:"logger"`
1213

1314
Pprof struct {

cmd/neofs-node/config/logger/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,14 @@ func Encoding(c *config.Config) string {
4343

4444
return EncodingDefault
4545
}
46+
47+
// Timestamp returns the value of "timestamp" config parameter
48+
// from "logger" section.
49+
//
50+
// Returns false if the value is missing or invalid.
51+
func Timestamp(c *config.Config) bool {
52+
return config.BoolSafe(
53+
c.Sub("logger"),
54+
"timestamp",
55+
)
56+
}

cmd/neofs-node/config/logger/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func TestLoggerSection_Level(t *testing.T) {
1414
emptyConfig := configtest.EmptyConfig()
1515
require.Equal(t, loggerconfig.LevelDefault, loggerconfig.Level(emptyConfig))
1616
require.Equal(t, loggerconfig.EncodingDefault, loggerconfig.Encoding(emptyConfig))
17+
require.False(t, loggerconfig.Timestamp(emptyConfig))
1718
})
1819

1920
const path = "../../../../config/example/node"

config/example/ir.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
NEOFS_IR_LOGGER_LEVEL=info
22
NEOFS_IR_LOGGER_ENCODING=console
3+
NEOFS_IR_LOGGER_TIMESTAMP=false
34

45
NEOFS_IR_WALLET_PATH=/path/to/wallet.json
56
NEOFS_IR_WALLET_ADDRESS=NUHtW3eM6a4mmFCgyyr4rj4wygsTKB88XX

config/example/ir.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
logger:
44
level: info # Logger level: one of "debug", "info" (default), "warn", "error", "dpanic", "panic", "fatal"
55
encoding: console # Logger encoding: one of "console" (default) or "json"
6+
timestamp: false # turn on/off timestamps. By default, timestamps are disabled,
7+
# but if the parameter is not set, they will be enabled when you run with tty)
68

79
wallet:
810
path: /path/to/wallet.json # Path to NEP-6 NEO wallet file

config/example/node.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
NEOFS_LOGGER_LEVEL=debug
22
NEOFS_LOGGER_ENCODING=json
3+
NEOFS_LOGGER_TIMESTAMP=true
34

45
NEOFS_PPROF_ENABLED=true
56
NEOFS_PPROF_ADDRESS=localhost:6060

0 commit comments

Comments
 (0)