Skip to content

Commit

Permalink
log: Hide timestamp if the program is not run in TTY
Browse files Browse the repository at this point in the history
Signed-off-by: Tatiana Nesterenko <tatiana@nspcc.io>
  • Loading branch information
tatiana-nspcc committed Jun 5, 2024
1 parent c965f16 commit 3c8b7ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cmd/neofs-rest-gw/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/term"
)

const (
Expand Down Expand Up @@ -437,7 +438,12 @@ func newLogger(v *viper.Viper) *zap.Logger {
c := zap.NewProductionConfig()
c.Level = zap.NewAtomicLevelAt(lvl)
c.Encoding = "console"
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
// If the program is run in TTY, the logger adds a timestamp to its entries.
if term.IsTerminal(int(os.Stdout.Fd())) {
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder

Check warning on line 443 in cmd/neofs-rest-gw/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-rest-gw/config.go#L443

Added line #L443 was not covered by tests
} else {
c.EncoderConfig.EncodeTime = func(t time.Time, encoder zapcore.PrimitiveArrayEncoder) {}
}

l, err := c.Build(
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ require (
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/term v0.18.0
golang.org/x/text v0.14.0 // indirect
google.golang.org/grpc v1.62.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
Expand Down

0 comments on commit 3c8b7ff

Please sign in to comment.