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/api/layer/neofs"
|
||||||
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
|
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
|
||||||
"github.com/nspcc-dev/neofs-s3-gw/internal/neofstest"
|
"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"
|
||||||
"github.com/nspcc-dev/neofs-sdk-go/object/address"
|
"github.com/nspcc-dev/neofs-sdk-go/object/address"
|
||||||
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
||||||
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
||||||
tokentest "github.com/nspcc-dev/neofs-sdk-go/token/test"
|
tokentest "github.com/nspcc-dev/neofs-sdk-go/token/test"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (tc *testContext) putObject(content []byte) *data.ObjectInfo {
|
func (tc *testContext) putObject(content []byte) *data.ObjectInfo {
|
||||||
|
@ -151,8 +151,6 @@ func prepareContext(t *testing.T, cachesConfig ...*CachesConfig) *testContext {
|
||||||
GateKey: key.PublicKey(),
|
GateKey: key.PublicKey(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
l, err := logger.New(logger.WithTraceLevel("panic"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
tp := neofstest.NewTestNeoFS()
|
tp := neofstest.NewTestNeoFS()
|
||||||
|
|
||||||
bktName := "testbucket1"
|
bktName := "testbucket1"
|
||||||
|
@ -173,7 +171,7 @@ func prepareContext(t *testing.T, cachesConfig ...*CachesConfig) *testContext {
|
||||||
|
|
||||||
return &testContext{
|
return &testContext{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
layer: NewLayer(l, tp, layerCfg),
|
layer: NewLayer(zap.NewNop(), tp, layerCfg),
|
||||||
bktInfo: &data.BucketInfo{
|
bktInfo: &data.BucketInfo{
|
||||||
Name: bktName,
|
Name: bktName,
|
||||||
Owner: owner.NewID(),
|
Owner: owner.NewID(),
|
||||||
|
|
|
@ -32,13 +32,7 @@ const (
|
||||||
|
|
||||||
const ( // Settings.
|
const ( // Settings.
|
||||||
// Logger.
|
// Logger.
|
||||||
cfgLoggerLevel = "logger.level"
|
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"
|
|
||||||
|
|
||||||
// Wallet.
|
// Wallet.
|
||||||
cfgWallet = "wallet"
|
cfgWallet = "wallet"
|
||||||
|
@ -106,8 +100,6 @@ const ( // Settings.
|
||||||
cfgResolveOrder = "resolve-order"
|
cfgResolveOrder = "resolve-order"
|
||||||
|
|
||||||
// Application.
|
// Application.
|
||||||
cfgApplicationName = "app.name"
|
|
||||||
cfgApplicationVersion = "app.version"
|
|
||||||
cfgApplicationBuildTime = "app.build_time"
|
cfgApplicationBuildTime = "app.build_time"
|
||||||
|
|
||||||
// Command line args.
|
// Command line args.
|
||||||
|
@ -115,16 +107,11 @@ const ( // Settings.
|
||||||
cmdVersion = "version"
|
cmdVersion = "version"
|
||||||
cmdConfig = "config"
|
cmdConfig = "config"
|
||||||
|
|
||||||
// applicationName is gateway name.
|
|
||||||
applicationName = "neofs-s3-gw"
|
|
||||||
|
|
||||||
// envPrefix is environment variables prefix used for configuration.
|
// envPrefix is environment variables prefix used for configuration.
|
||||||
envPrefix = "S3_GW"
|
envPrefix = "S3_GW"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ignore = map[string]struct{}{
|
var ignore = map[string]struct{}{
|
||||||
cfgApplicationName: {},
|
|
||||||
cfgApplicationVersion: {},
|
|
||||||
cfgApplicationBuildTime: {},
|
cfgApplicationBuildTime: {},
|
||||||
|
|
||||||
cfgPeers: {},
|
cfgPeers: {},
|
||||||
|
@ -223,20 +210,10 @@ func newSettings() *viper.Viper {
|
||||||
|
|
||||||
domains := flags.StringArrayP(cfgListenDomains, "d", nil, "set domains to be listened")
|
domains := flags.StringArrayP(cfgListenDomains, "d", nil, "set domains to be listened")
|
||||||
|
|
||||||
// set prefers:
|
|
||||||
v.Set(cfgApplicationName, applicationName)
|
|
||||||
v.Set(cfgApplicationVersion, version.Version)
|
|
||||||
|
|
||||||
// set defaults:
|
// set defaults:
|
||||||
|
|
||||||
// logger:
|
// logger:
|
||||||
v.SetDefault(cfgLoggerLevel, "debug")
|
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 {
|
if err := v.BindPFlags(flags); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
@ -2,42 +2,63 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neofs-sdk-go/logger"
|
"github.com/nspcc-dev/neofs-s3-gw/internal/version"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.uber.org/zap"
|
"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 {
|
func newLogger(v *viper.Viper) *zap.Logger {
|
||||||
options := []logger.Option{
|
var lvl zapcore.Level
|
||||||
logger.WithLevel(v.GetString(cfgLoggerLevel)),
|
lvlStr := v.GetString(cfgLoggerLevel)
|
||||||
logger.WithTraceLevel(v.GetString(cfgLoggerTraceLevel)),
|
|
||||||
|
|
||||||
logger.WithFormat(v.GetString(cfgLoggerFormat)),
|
err := lvl.UnmarshalText([]byte(lvlStr))
|
||||||
|
|
||||||
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...)
|
|
||||||
if err != nil {
|
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() {
|
func main() {
|
||||||
|
|
Loading…
Reference in a new issue