Skip to content

Commit

Permalink
*: Use go.uber.org/zap.Logger everywhere
Browse files Browse the repository at this point in the history
Nothing foreshadows leaving zap logger in the near future.

Closes #696.

Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Jul 21, 2023
1 parent f751d71 commit df84476
Show file tree
Hide file tree
Showing 112 changed files with 300 additions and 527 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Changelog for NeoFS Node
- Deprecated `morph.rpc_endpoint` SN and `morph.endpoint.client` IR config sections (#2400)
- `neofs_node_object_epoch` metric for IR and SN (#2347)
- Subnets support (#2411)
- Logging utility completely replaced with `zap.Logger` (#696)

### Changed
- CLI `--timeout` flag configures whole execution timeout from now (#2124)
Expand Down
19 changes: 11 additions & 8 deletions cmd/neofs-ir/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/nspcc-dev/neofs-node/misc"
"github.com/nspcc-dev/neofs-node/pkg/innerring"
httputil "github.com/nspcc-dev/neofs-node/pkg/util/http"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

const (
Expand Down Expand Up @@ -47,14 +47,17 @@ func main() {
cfg, err := newConfig(*configFile)
exitErr(err)

var logPrm logger.Prm

err = logPrm.SetLevelString(
cfg.GetString("logger.level"),
)
logLevel, err := zap.ParseAtomicLevel(cfg.GetString("logger.level"))
exitErr(err)

log, err := logger.NewLogger(&logPrm)
c := zap.NewProductionConfig()
c.Level = logLevel
c.Encoding = "console"
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder

log, err := c.Build(
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
)
exitErr(err)

ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
Expand Down Expand Up @@ -107,7 +110,7 @@ func main() {
log.Info("application stopped")
}

func initHTTPServers(cfg *viper.Viper, log *logger.Logger) []*httputil.Server {
func initHTTPServers(cfg *viper.Viper, log *zap.Logger) []*httputil.Server {
items := []struct {
cfgPrefix string
handler func() http.Handler
Expand Down
68 changes: 18 additions & 50 deletions cmd/neofs-node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/services/tree"
"github.com/nspcc-dev/neofs-node/pkg/services/util/response"
"github.com/nspcc-dev/neofs-node/pkg/util"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"github.com/nspcc-dev/neofs-node/pkg/util/state"
neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa"
"github.com/nspcc-dev/neofs-sdk-go/netmap"
Expand All @@ -71,6 +70,7 @@ import (
"go.etcd.io/bbolt"
"go.uber.org/atomic"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -306,7 +306,8 @@ type internals struct {

appCfg *config.Config

log *logger.Logger
logLevel zap.AtomicLevel
log *zap.Logger

wg *sync.WaitGroup
workers []worker
Expand Down Expand Up @@ -394,7 +395,6 @@ func (s shared) resetCaches() {
// dynamicConfiguration stores parameters of the
// components that supports runtime reconfigurations.
type dynamicConfiguration struct {
logger *logger.Prm
}

type cfg struct {
Expand Down Expand Up @@ -546,12 +546,6 @@ func initCfg(appCfg *config.Config) *cfg {

key := nodeconfig.Key(appCfg)

logPrm, err := c.loggerPrm()
fatalOnErr(err)

log, err := logger.NewLogger(logPrm)
fatalOnErr(err)

var netAddr network.AddressGroup

relayOnly := nodeconfig.Relay(appCfg)
Expand Down Expand Up @@ -580,12 +574,24 @@ func initCfg(appCfg *config.Config) *cfg {
ctx: context.Background(),
appCfg: appCfg,
internalErr: make(chan error, 10), // We only need one error, but we can have multiple senders.
log: log,
wg: new(sync.WaitGroup),
apiVersion: version.Current(),
healthStatus: atomic.NewInt32(int32(control.HealthStatus_HEALTH_STATUS_UNDEFINED)),
}

c.internals.logLevel, err = zap.ParseAtomicLevel(c.LoggerCfg.level)
fatalOnErr(err)

logCfg := zap.NewProductionConfig()
logCfg.Level = c.internals.logLevel
logCfg.Encoding = "console"
logCfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder

c.internals.log, err = logCfg.Build(
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
)
fatalOnErr(err)

cacheOpts := cache.ClientCacheOpts{
DialTimeout: apiclientconfig.DialTimeout(appCfg),
StreamTimeout: apiclientconfig.StreamTimeout(appCfg),
Expand Down Expand Up @@ -780,22 +786,6 @@ func (c *cfg) shardOpts() []shardOptsWithID {
return shards
}

func (c *cfg) loggerPrm() (*logger.Prm, error) {
// check if it has been inited before
if c.dynamicConfiguration.logger == nil {
c.dynamicConfiguration.logger = new(logger.Prm)
}

// (re)init read configuration
err := c.dynamicConfiguration.logger.SetLevelString(c.LoggerCfg.level)
if err != nil {
// not expected since validation should be performed before
panic(fmt.Sprintf("incorrect log level format: %s", c.LoggerCfg.level))
}

return c.dynamicConfiguration.logger, nil
}

func (c *cfg) LocalAddress() network.AddressGroup {
return c.localAddr
}
Expand Down Expand Up @@ -940,13 +930,6 @@ func (c *cfg) ObjectServiceLoad() float64 {
return float64(c.cfgObject.pool.putRemote.Running()) / float64(c.cfgObject.pool.putRemoteCapacity)
}

type dCfg struct {
name string
cfg interface {
Reload() error
}
}

func (c *cfg) configWatcher(ctx context.Context) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGHUP)
Expand All @@ -962,20 +945,14 @@ func (c *cfg) configWatcher(ctx context.Context) {
continue
}

// all the components are expected to support
// Logger's dynamic reconfiguration approach
var components []dCfg

// Logger

logPrm, err := c.loggerPrm()
err = c.internals.logLevel.UnmarshalText([]byte(c.LoggerCfg.level))
if err != nil {
c.log.Error("logger configuration preparation", zap.Error(err))
c.log.Error("invalid logger level configuration", zap.Error(err))
continue
}

components = append(components, dCfg{name: "logger", cfg: logPrm})

// Storage Engine

var rcfg engine.ReConfiguration
Expand All @@ -989,15 +966,6 @@ func (c *cfg) configWatcher(ctx context.Context) {
continue
}

for _, component := range components {
err = component.cfg.Reload()
if err != nil {
c.log.Error("updated configuration applying",
zap.String("component", component.name),
zap.Error(err))
}
}

c.log.Info("configuration has been reloaded successfully")
case <-ctx.Done():
return
Expand Down
7 changes: 3 additions & 4 deletions cmd/neofs-node/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
placementrouter "github.com/nspcc-dev/neofs-node/pkg/services/container/announcement/load/route/placement"
loadstorage "github.com/nspcc-dev/neofs-node/pkg/services/container/announcement/load/storage"
containerMorph "github.com/nspcc-dev/neofs-node/pkg/services/container/morph"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
apiClient "github.com/nspcc-dev/neofs-sdk-go/client"
containerSDK "github.com/nspcc-dev/neofs-sdk-go/container"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
Expand Down Expand Up @@ -281,7 +280,7 @@ func setContainerNotificationParser(c *cfg, sTyp string, p event.NotificationPar
}

type morphLoadWriter struct {
log *logger.Logger
log *zap.Logger

cnrMorphClient *cntClient.Client

Expand Down Expand Up @@ -387,7 +386,7 @@ func (r *remoteLoadAnnounceWriter) Close() error {
}

type loadPlacementBuilder struct {
log *logger.Logger
log *zap.Logger

nmSrc netmapCore.Source

Expand Down Expand Up @@ -517,7 +516,7 @@ func (l *loadPlacementBuilder) buildPlacement(epoch uint64, idCnr cid.ID) ([][]n
}

type localStorageLoad struct {
log *logger.Logger
log *zap.Logger

engine *engine.StorageEngine
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/neofs-node/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

grpcconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/grpc"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -104,8 +103,8 @@ func serveGRPC(c *cfg) {
}
}

func stopGRPC(name string, s *grpc.Server, l *logger.Logger) {
l = &logger.Logger{Logger: l.With(zap.String("name", name))}
func stopGRPC(name string, s *grpc.Server, l *zap.Logger) {
l = l.With(zap.String("name", name))

l.Info("stopping gRPC server...")

Expand Down
5 changes: 2 additions & 3 deletions cmd/neofs-node/notificator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
"github.com/nspcc-dev/neofs-node/pkg/services/notificator"
"github.com/nspcc-dev/neofs-node/pkg/services/notificator/nats"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"go.uber.org/zap"
)

type notificationSource struct {
e *engine.StorageEngine
l *logger.Logger
l *zap.Logger
defaultTopic string
}

Expand Down Expand Up @@ -93,7 +92,7 @@ func (n *notificationSource) processAddress(
}

type notificationWriter struct {
l *logger.Logger
l *zap.Logger
w *nats.Writer
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/neofs-node/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/services/policer"
"github.com/nspcc-dev/neofs-node/pkg/services/replicator"
truststorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/storage"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"github.com/nspcc-dev/neofs-sdk-go/client"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
eaclSDK "github.com/nspcc-dev/neofs-sdk-go/eacl"
Expand Down Expand Up @@ -395,7 +394,7 @@ func (s *morphEACLFetcher) GetEACL(cnr cid.ID) (*containercore.EACL, error) {
}

type reputationClientConstructor struct {
log *logger.Logger
log *zap.Logger

nmSrc netmap.Source

Expand Down
7 changes: 3 additions & 4 deletions cmd/neofs-node/reputation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
localroutes "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/routes"
truststorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/storage"
reputationrpc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/rpc"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
apireputation "github.com/nspcc-dev/neofs-sdk-go/reputation"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -60,8 +59,8 @@ func initReputationService(c *cfg) {
Storage: consumerStorage,
}

localTrustLogger := &logger.Logger{Logger: c.log.With(zap.String("trust_type", "local"))}
intermediateTrustLogger := &logger.Logger{Logger: c.log.With(zap.String("trust_type", "intermediate"))}
localTrustLogger := c.log.With(zap.String("trust_type", "local"))
intermediateTrustLogger := c.log.With(zap.String("trust_type", "intermediate"))

localTrustStorage := &localreputation.TrustStorage{
Log: localTrustLogger,
Expand Down Expand Up @@ -256,7 +255,7 @@ func initReputationService(c *cfg) {

type reputationServer struct {
*cfg
log *logger.Logger
log *zap.Logger
localRouter reputationcommon.WriterProvider
intermediateRouter reputationcommon.WriterProvider
routeBuilder reputationrouter.Builder
Expand Down
6 changes: 3 additions & 3 deletions cmd/neofs-node/reputation/common/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
reputationcommon "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
reputationrouter "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common/router"
trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"go.uber.org/zap"
)

type clientCache interface {
Expand All @@ -32,7 +32,7 @@ type remoteTrustProvider struct {
deadEndProvider reputationcommon.WriterProvider
clientCache clientCache
remoteProvider clientKeyRemoteProvider
log *logger.Logger
log *zap.Logger
}

// RemoteProviderPrm groups the required parameters of the remoteTrustProvider's constructor.
Expand All @@ -45,7 +45,7 @@ type RemoteProviderPrm struct {
DeadEndProvider reputationcommon.WriterProvider
ClientCache clientCache
WriterProvider clientKeyRemoteProvider
Log *logger.Logger
Log *zap.Logger
}

func NewRemoteTrustProvider(prm RemoteProviderPrm) reputationrouter.RemoteWriterProvider {
Expand Down
5 changes: 2 additions & 3 deletions cmd/neofs-node/reputation/intermediate/consumers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust"
eigencalc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/calculator"
consumerstorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/storage/consumers"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"go.uber.org/zap"
)

Expand All @@ -15,15 +14,15 @@ var ErrIncorrectContextPanicMsg = "could not write intermediate trust: passed co
// ConsumerStorageWriterProvider is an implementation of the reputation.WriterProvider
// interface that provides ConsumerTrustWriter writer.
type ConsumerStorageWriterProvider struct {
Log *logger.Logger
Log *zap.Logger
Storage *consumerstorage.Storage
}

// ConsumerTrustWriter is an implementation of the reputation.Writer interface
// that writes passed consumer's Trust values to the Consumer storage. After writing
// that, values can be used in eigenTrust algorithm's iterations.
type ConsumerTrustWriter struct {
log *logger.Logger
log *zap.Logger
storage *consumerstorage.Storage
eiCtx eigencalc.Context
}
Expand Down
Loading

0 comments on commit df84476

Please sign in to comment.