From 6e325277153329b331636c54f6224fb5c63f7c4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20R=C3=B3=C5=BCa=C5=84ski?= Date: Mon, 5 Aug 2024 13:56:58 +0000 Subject: [PATCH] use zap in bootstrap (#6212) ## Motivation Part of the effort to migrate to zap. --- bootstrap/updater.go | 27 ++++++++++++++------------- bootstrap/updater_test.go | 24 ++++++++++++------------ node/node.go | 2 +- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/bootstrap/updater.go b/bootstrap/updater.go index 04f53cbdd2..9641aefb96 100644 --- a/bootstrap/updater.go +++ b/bootstrap/updater.go @@ -29,6 +29,7 @@ import ( "github.com/santhosh-tekuri/jsonschema/v5" "github.com/spf13/afero" + "go.uber.org/zap" "golang.org/x/sync/errgroup" "github.com/spacemeshos/go-spacemesh/common/types" @@ -71,7 +72,7 @@ func DefaultConfig() Config { type Updater struct { cfg Config - logger log.Log + logger *zap.Logger clock layerClock fs afero.Fs client *http.Client @@ -92,7 +93,7 @@ func WithConfig(cfg Config) Opt { } } -func WithLogger(logger log.Log) Opt { +func WithLogger(logger *zap.Logger) Opt { return func(u *Updater) { u.logger = logger } @@ -113,7 +114,7 @@ func WithHttpClient(c *http.Client) Opt { func New(clock layerClock, opts ...Opt) *Updater { u := &Updater{ cfg: DefaultConfig(), - logger: log.NewNop(), + logger: zap.NewNop(), clock: clock, fs: afero.NewOsFs(), client: &http.Client{}, @@ -149,7 +150,7 @@ func (u *Updater) Load(ctx context.Context) error { if err = u.updateAndNotify(ctx, verified); err != nil { return err } - u.logger.With().Info("loaded bootstrap file", log.Inline(verified)) + u.logger.Info("loaded bootstrap file", zap.Inline(verified)) u.addUpdate(verified.Data.Epoch, verified.Persisted[len(verified.Persisted)-suffixLen:]) } return nil @@ -165,14 +166,14 @@ func (u *Updater) Start() error { if err := u.Load(ctx); err != nil { return err } - u.logger.With().Info("start listening to update", - log.String("source", u.cfg.URL), - log.Duration("interval", u.cfg.Interval), + u.logger.Info("start listening to update", + zap.String("source", u.cfg.URL), + zap.Duration("interval", u.cfg.Interval), ) for { if err := u.DoIt(ctx); err != nil { updateFailureCount.Add(1) - u.logger.With().Debug("failed to get bootstrap update", log.Err(err)) + u.logger.Debug("failed to get bootstrap update", zap.Error(err)) } select { case <-u.stop: @@ -233,10 +234,10 @@ func (u *Updater) DoIt(ctx context.Context) error { current := u.clock.CurrentLayer().GetEpoch() defer func() { if err := u.prune(current); err != nil { - u.logger.With().Error("failed to prune", - log.Context(ctx), - log.Uint32("current epoch", current.Uint32()), - log.Err(err), + u.logger.Error("failed to prune", + log.ZContext(ctx), + zap.Uint32("current epoch", current.Uint32()), + zap.Error(err), ) } }() @@ -291,7 +292,7 @@ func (u *Updater) checkEpochUpdate( return nil, false, fmt.Errorf("persist bootstrap %s: %w", filename, err) } verified.Persisted = filename - u.logger.WithContext(ctx).With().Info("new bootstrap file", log.Inline(verified)) + u.logger.Info("new bootstrap file", log.ZContext(ctx), zap.Inline(verified)) if err = u.updateAndNotify(ctx, verified); err != nil { return verified, false, err } diff --git a/bootstrap/updater_test.go b/bootstrap/updater_test.go index 2cc98e4c4d..4a3b712b36 100644 --- a/bootstrap/updater_test.go +++ b/bootstrap/updater_test.go @@ -15,11 +15,11 @@ import ( "github.com/spf13/afero" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" + "go.uber.org/zap/zaptest" "golang.org/x/sync/errgroup" "github.com/spacemeshos/go-spacemesh/bootstrap" "github.com/spacemeshos/go-spacemesh/common/types" - "github.com/spacemeshos/go-spacemesh/log/logtest" ) const ( @@ -196,7 +196,7 @@ func TestLoad(t *testing.T) { updater := bootstrap.New( mc, bootstrap.WithConfig(cfg), - bootstrap.WithLogger(logtest.New(t)), + bootstrap.WithLogger(zaptest.NewLogger(t)), bootstrap.WithFilesystem(fs), ) ch, err := updater.Subscribe() @@ -248,7 +248,7 @@ func TestLoadedNotDownloadedAgain(t *testing.T) { updater := bootstrap.New( mc, bootstrap.WithConfig(cfg), - bootstrap.WithLogger(logtest.New(t)), + bootstrap.WithLogger(zaptest.NewLogger(t)), bootstrap.WithFilesystem(fs), ) ch, err := updater.Subscribe() @@ -276,7 +276,7 @@ func TestStartClose(t *testing.T) { updater := bootstrap.New( mc, bootstrap.WithConfig(cfg), - bootstrap.WithLogger(logtest.New(t)), + bootstrap.WithLogger(zaptest.NewLogger(t)), bootstrap.WithFilesystem(fs), ) ch, err := updater.Subscribe() @@ -322,7 +322,7 @@ func TestPrune(t *testing.T) { updater := bootstrap.New( mc, bootstrap.WithConfig(cfg), - bootstrap.WithLogger(logtest.New(t)), + bootstrap.WithLogger(zaptest.NewLogger(t)), bootstrap.WithFilesystem(fs), bootstrap.WithHttpClient(ts.Client()), ) @@ -384,7 +384,7 @@ func TestDoIt(t *testing.T) { updater := bootstrap.New( mc, bootstrap.WithConfig(cfg), - bootstrap.WithLogger(logtest.New(t)), + bootstrap.WithLogger(zaptest.NewLogger(t)), bootstrap.WithFilesystem(fs), bootstrap.WithHttpClient(ts.Client()), ) @@ -421,7 +421,7 @@ func TestEmptyResponse(t *testing.T) { updater := bootstrap.New( mc, bootstrap.WithConfig(cfg), - bootstrap.WithLogger(logtest.New(t)), + bootstrap.WithLogger(zaptest.NewLogger(t)), bootstrap.WithFilesystem(fs), bootstrap.WithHttpClient(ts.Client()), ) @@ -492,7 +492,7 @@ func TestGetInvalidUpdate(t *testing.T) { updater := bootstrap.New( mc, bootstrap.WithConfig(cfg), - bootstrap.WithLogger(logtest.New(t)), + bootstrap.WithLogger(zaptest.NewLogger(t)), bootstrap.WithFilesystem(fs), bootstrap.WithHttpClient(ts.Client()), ) @@ -526,7 +526,7 @@ func TestNoNewUpdate(t *testing.T) { updater := bootstrap.New( mc, bootstrap.WithConfig(cfg), - bootstrap.WithLogger(logtest.New(t)), + bootstrap.WithLogger(zaptest.NewLogger(t)), bootstrap.WithFilesystem(fs), bootstrap.WithHttpClient(ts.Client()), ) @@ -647,7 +647,7 @@ func TestRequiredEpochs(t *testing.T) { updater := bootstrap.New( mc, bootstrap.WithConfig(cfg), - bootstrap.WithLogger(logtest.New(t)), + bootstrap.WithLogger(zaptest.NewLogger(t)), bootstrap.WithFilesystem(fs), bootstrap.WithHttpClient(ts.Client()), ) @@ -676,7 +676,7 @@ func TestIntegration(t *testing.T) { updater := bootstrap.New( mc, bootstrap.WithConfig(cfg), - bootstrap.WithLogger(logtest.New(t)), + bootstrap.WithLogger(zaptest.NewLogger(t)), bootstrap.WithFilesystem(fs), ) ch, err := updater.Subscribe() @@ -709,7 +709,7 @@ func TestClose(t *testing.T) { updater := bootstrap.New( mc, bootstrap.WithConfig(cfg), - bootstrap.WithLogger(logtest.New(t)), + bootstrap.WithLogger(zaptest.NewLogger(t)), bootstrap.WithFilesystem(fs), ) ch, err := updater.Subscribe() diff --git a/node/node.go b/node/node.go index 8b239cc2fa..195a9ec938 100644 --- a/node/node.go +++ b/node/node.go @@ -784,7 +784,7 @@ func (app *App) initServices(ctx context.Context) error { app.updater = bootstrap.New( app.clock, bootstrap.WithConfig(bscfg), - bootstrap.WithLogger(app.addLogger(BootstrapLogger, lg)), + bootstrap.WithLogger(app.addLogger(BootstrapLogger, lg).Zap()), ) if app.Config.Certificate.CommitteeSize == 0 { app.log.With().Warning("certificate committee size is not set, defaulting to hare committee size",