forked from TrueCloudLab/frostfs-s3-gw
[#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 <leonard@nspcc.ru>
This commit is contained in:
parent
f3df5ff633
commit
01c721ee53
3 changed files with 48 additions and 52 deletions
|
@ -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(),
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue