From 01c721ee531bba16dc55788472c6e03513d545bd Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 22 Mar 2022 13:22:00 +0300 Subject: [PATCH] [#380] cmd/s3-gw: Refactor logger Do not import `logger` package from NeoFS SDK Go. Discard unusable `name` and `version` configuration values from `app` section. Discard all unusable onfiguration values from `logger` section except `level`. Signed-off-by: Leonard Lyubich --- api/layer/versioning_test.go | 6 ++-- cmd/s3-gw/app_settings.go | 25 +------------ cmd/s3-gw/main.go | 69 +++++++++++++++++++++++------------- 3 files changed, 48 insertions(+), 52 deletions(-) diff --git a/api/layer/versioning_test.go b/api/layer/versioning_test.go index 1487169c..69c0d949 100644 --- a/api/layer/versioning_test.go +++ b/api/layer/versioning_test.go @@ -13,13 +13,13 @@ import ( "github.com/nspcc-dev/neofs-s3-gw/api/layer/neofs" "github.com/nspcc-dev/neofs-s3-gw/creds/accessbox" "github.com/nspcc-dev/neofs-s3-gw/internal/neofstest" - "github.com/nspcc-dev/neofs-sdk-go/logger" "github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/object/address" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "github.com/nspcc-dev/neofs-sdk-go/owner" tokentest "github.com/nspcc-dev/neofs-sdk-go/token/test" "github.com/stretchr/testify/require" + "go.uber.org/zap" ) func (tc *testContext) putObject(content []byte) *data.ObjectInfo { @@ -151,8 +151,6 @@ func prepareContext(t *testing.T, cachesConfig ...*CachesConfig) *testContext { GateKey: key.PublicKey(), }, }) - l, err := logger.New(logger.WithTraceLevel("panic")) - require.NoError(t, err) tp := neofstest.NewTestNeoFS() bktName := "testbucket1" @@ -173,7 +171,7 @@ func prepareContext(t *testing.T, cachesConfig ...*CachesConfig) *testContext { return &testContext{ ctx: ctx, - layer: NewLayer(l, tp, layerCfg), + layer: NewLayer(zap.NewNop(), tp, layerCfg), bktInfo: &data.BucketInfo{ Name: bktName, Owner: owner.NewID(), diff --git a/cmd/s3-gw/app_settings.go b/cmd/s3-gw/app_settings.go index 9dc60398..2262da24 100644 --- a/cmd/s3-gw/app_settings.go +++ b/cmd/s3-gw/app_settings.go @@ -32,13 +32,7 @@ const ( const ( // Settings. // Logger. - cfgLoggerLevel = "logger.level" - cfgLoggerFormat = "logger.format" - cfgLoggerTraceLevel = "logger.trace_level" - cfgLoggerNoCaller = "logger.no_caller" - cfgLoggerNoDisclaimer = "logger.no_disclaimer" - cfgLoggerSamplingInitial = "logger.sampling.initial" - cfgLoggerSamplingThereafter = "logger.sampling.thereafter" + cfgLoggerLevel = "logger.level" // Wallet. cfgWallet = "wallet" @@ -106,8 +100,6 @@ const ( // Settings. cfgResolveOrder = "resolve-order" // Application. - cfgApplicationName = "app.name" - cfgApplicationVersion = "app.version" cfgApplicationBuildTime = "app.build_time" // Command line args. @@ -115,16 +107,11 @@ const ( // Settings. cmdVersion = "version" cmdConfig = "config" - // applicationName is gateway name. - applicationName = "neofs-s3-gw" - // envPrefix is environment variables prefix used for configuration. envPrefix = "S3_GW" ) var ignore = map[string]struct{}{ - cfgApplicationName: {}, - cfgApplicationVersion: {}, cfgApplicationBuildTime: {}, cfgPeers: {}, @@ -223,20 +210,10 @@ func newSettings() *viper.Viper { domains := flags.StringArrayP(cfgListenDomains, "d", nil, "set domains to be listened") - // set prefers: - v.Set(cfgApplicationName, applicationName) - v.Set(cfgApplicationVersion, version.Version) - // set defaults: // logger: v.SetDefault(cfgLoggerLevel, "debug") - v.SetDefault(cfgLoggerFormat, "console") - v.SetDefault(cfgLoggerTraceLevel, "panic") - v.SetDefault(cfgLoggerNoCaller, false) - v.SetDefault(cfgLoggerNoDisclaimer, true) - v.SetDefault(cfgLoggerSamplingInitial, 1000) - v.SetDefault(cfgLoggerSamplingThereafter, 1000) if err := v.BindPFlags(flags); err != nil { panic(err) diff --git a/cmd/s3-gw/main.go b/cmd/s3-gw/main.go index 520a9280..41f2e1aa 100644 --- a/cmd/s3-gw/main.go +++ b/cmd/s3-gw/main.go @@ -2,42 +2,63 @@ package main import ( "context" + "fmt" "os/signal" "syscall" - "github.com/nspcc-dev/neofs-sdk-go/logger" + "github.com/nspcc-dev/neofs-s3-gw/internal/version" "github.com/spf13/viper" "go.uber.org/zap" + "go.uber.org/zap/zapcore" ) +// newLogger constructs a zap.Logger instance for current application. +// Panics on failure. +// +// Logger is built from zap's production logging configuration with: +// * parameterized level (debug by default) +// * console encoding +// * ISO8601 time encoding +// * app_name field set to neofs-s3-gw +// * app_version field set to version.Version +// +// Logger records a stack trace for all messages at or above fatal level. +// +// See also zapcore.Level, zap.NewProductionConfig, zap.AddStacktrace. func newLogger(v *viper.Viper) *zap.Logger { - options := []logger.Option{ - logger.WithLevel(v.GetString(cfgLoggerLevel)), - logger.WithTraceLevel(v.GetString(cfgLoggerTraceLevel)), + var lvl zapcore.Level + lvlStr := v.GetString(cfgLoggerLevel) - logger.WithFormat(v.GetString(cfgLoggerFormat)), - - logger.WithSamplingInitial(v.GetInt(cfgLoggerSamplingInitial)), - logger.WithSamplingThereafter(v.GetInt(cfgLoggerSamplingThereafter)), - - logger.WithAppName(v.GetString(cfgApplicationName)), - logger.WithAppVersion(v.GetString(cfgApplicationVersion)), - } - - if v.GetBool(cfgLoggerNoCaller) { - options = append(options, logger.WithoutCaller()) - } - - if v.GetBool(cfgLoggerNoDisclaimer) { - options = append(options, logger.WithoutDisclaimer()) - } - - l, err := logger.New(options...) + err := lvl.UnmarshalText([]byte(lvlStr)) if err != nil { - panic(err) + panic(fmt.Sprintf("incorrect logger level configuration %s (%v), "+ + "value should be one of %v", lvlStr, err, [...]zapcore.Level{ + zapcore.DebugLevel, + zapcore.InfoLevel, + zapcore.WarnLevel, + zapcore.ErrorLevel, + zapcore.DPanicLevel, + zapcore.PanicLevel, + zapcore.FatalLevel, + })) } - return l + c := zap.NewProductionConfig() + c.Level = zap.NewAtomicLevelAt(lvl) + c.Encoding = "console" + c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + + l, err := c.Build( + zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)), + ) + if err != nil { + panic(fmt.Sprintf("build zap logger instance: %v", err)) + } + + return l.With( + zap.String("app_name", "neofs-s3-gw"), + zap.String("app_version", version.Version), + ) } func main() {