diff --git a/cmd/bootstrapper/bootstrapper.go b/cmd/bootstrapper/bootstrapper.go index 299d72d8ce..af6bf53e17 100644 --- a/cmd/bootstrapper/bootstrapper.go +++ b/cmd/bootstrapper/bootstrapper.go @@ -23,7 +23,6 @@ import ( "google.golang.org/grpc/credentials/insecure" "github.com/spacemeshos/go-spacemesh/common/types" - "github.com/spacemeshos/go-spacemesh/log" ) const ( @@ -89,16 +88,26 @@ var cmd = &cobra.Command{ targetEpochs = append(targetEpochs, types.EpochID(epoch)) } - log.JSONLog(true) - lvl, err := zap.ParseAtomicLevel(strings.ToLower(logLevel)) + lvl, err := zap.ParseAtomicLevel(logLevel) if err != nil { return err } - logger := log.NewWithLevel("", lvl) + + logger, err := zap.Config{ + Level: lvl, + Encoding: "json", + EncoderConfig: zap.NewProductionEncoderConfig(), + OutputPaths: []string{"stderr"}, + ErrorOutputPaths: []string{"stderr"}, + }.Build() + if err != nil { + return fmt.Errorf("creating logger: %w", err) + } + g := NewGenerator( bitcoinEndpoint, spacemeshEndpoint, - WithLogger(logger.WithName("generator")), + WithLogger(logger.Named("generator")), ) ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) @@ -106,7 +115,7 @@ var cmd = &cobra.Command{ if serveUpdate { srv := NewServer(g, genFallback, port, WithSrvFilesystem(afero.NewOsFs()), - WithSrvLogger(logger.WithName("server")), + WithSrvLogger(logger.Named("server")), WithBootstrapEpochs(targetEpochs), ) return runServer(ctx, srv) diff --git a/cmd/bootstrapper/generator.go b/cmd/bootstrapper/generator.go index c6643885b4..0c8ac6456f 100644 --- a/cmd/bootstrapper/generator.go +++ b/cmd/bootstrapper/generator.go @@ -16,12 +16,12 @@ import ( pb "github.com/spacemeshos/api/release/go/spacemesh/v1" "github.com/spf13/afero" + "go.uber.org/zap" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "github.com/spacemeshos/go-spacemesh/bootstrap" "github.com/spacemeshos/go-spacemesh/common/types" - "github.com/spacemeshos/go-spacemesh/log" ) const ( @@ -35,7 +35,7 @@ func PersistedFilename(epoch types.EpochID, suffix string) string { } type Generator struct { - logger log.Log + logger *zap.Logger fs afero.Fs client *http.Client btcEndpoint string @@ -44,7 +44,7 @@ type Generator struct { type Opt func(*Generator) -func WithLogger(logger log.Log) Opt { +func WithLogger(logger *zap.Logger) Opt { return func(g *Generator) { g.logger = logger } @@ -64,7 +64,7 @@ func WithHttpClient(c *http.Client) Opt { func NewGenerator(btcEndpoint, smEndpoint string, opts ...Opt) *Generator { g := &Generator{ - logger: log.NewNop(), + logger: zap.NewNop(), fs: afero.NewOsFs(), client: &http.Client{}, btcEndpoint: btcEndpoint, @@ -125,7 +125,7 @@ type BitcoinResponse struct { Hash string `json:"hash"` } -func (g *Generator) genBeacon(ctx context.Context, logger log.Log) (types.Beacon, error) { +func (g *Generator) genBeacon(ctx context.Context, logger *zap.Logger) (types.Beacon, error) { ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() br, err := bitcoinHash(ctx, logger, g.client, g.btcEndpoint) @@ -142,15 +142,17 @@ func (g *Generator) genBeacon(ctx context.Context, logger log.Log) (types.Beacon return beacon, nil } -func bitcoinHash(ctx context.Context, logger log.Log, client *http.Client, targetUrl string) (*BitcoinResponse, error) { +func bitcoinHash( + ctx context.Context, + logger *zap.Logger, + client *http.Client, + targetUrl string, +) (*BitcoinResponse, error) { latest, err := queryBitcoin(ctx, client, targetUrl) if err != nil { return nil, err } - logger.With().Info("latest bitcoin block height", - log.Uint64("height", latest.Height), - log.String("hash", latest.Hash), - ) + logger.Info("latest bitcoin block height", zap.Uint64("height", latest.Height), zap.String("hash", latest.Hash)) height := latest.Height - confirmation blockUrl := fmt.Sprintf("%s/blocks/%d", targetUrl, height) @@ -158,10 +160,7 @@ func bitcoinHash(ctx context.Context, logger log.Log, client *http.Client, targe if err != nil { return nil, err } - logger.With().Info("confirmed bitcoin block", - log.Uint64("height", confirmed.Height), - log.String("hash", confirmed.Hash), - ) + logger.Info("confirmed bitcoin block", zap.Uint64("height", confirmed.Height), zap.String("hash", confirmed.Hash)) return confirmed, nil } @@ -257,8 +256,8 @@ func (g *Generator) GenUpdate( if err != nil { return "", fmt.Errorf("persist epoch update %v: %w", filename, err) } - g.logger.With().Info("generated update", - log.String("filename", filename), + g.logger.Info("generated update", + zap.String("filename", filename), ) return filename, nil } diff --git a/cmd/bootstrapper/generator_test.go b/cmd/bootstrapper/generator_test.go index 346e57209d..4c1aec7bb4 100644 --- a/cmd/bootstrapper/generator_test.go +++ b/cmd/bootstrapper/generator_test.go @@ -22,7 +22,6 @@ import ( "github.com/spacemeshos/go-spacemesh/bootstrap" "github.com/spacemeshos/go-spacemesh/common/types" "github.com/spacemeshos/go-spacemesh/datastore" - "github.com/spacemeshos/go-spacemesh/log/logtest" "github.com/spacemeshos/go-spacemesh/sql" "github.com/spacemeshos/go-spacemesh/sql/atxs" ) @@ -143,7 +142,7 @@ func TestGenerator_Generate(t *testing.T) { g := NewGenerator( ts.URL, cfg.PublicListener, - WithLogger(logtest.New(t)), + WithLogger(zaptest.NewLogger(t)), WithFilesystem(fs), WithHttpClient(ts.Client()), ) @@ -170,9 +169,9 @@ func TestGenerator_CheckAPI(t *testing.T) { t.Parallel() targetEpoch := types.EpochID(3) db := sql.InMemory() - lg := logtest.New(t) + lg := zaptest.NewLogger(t) createAtxs(t, db, targetEpoch-1, types.RandomActiveSet(activeSetSize)) - cfg, cleanup := launchServer(t, datastore.NewCachedDB(db, lg.Zap())) + cfg, cleanup := launchServer(t, datastore.NewCachedDB(db, lg)) t.Cleanup(cleanup) fs := afero.NewMemMapFs() diff --git a/cmd/bootstrapper/server.go b/cmd/bootstrapper/server.go index eb6fa5204f..a2c737451e 100644 --- a/cmd/bootstrapper/server.go +++ b/cmd/bootstrapper/server.go @@ -13,11 +13,11 @@ import ( "time" "github.com/spf13/afero" + "go.uber.org/zap" "golang.org/x/sync/errgroup" "github.com/spacemeshos/go-spacemesh/bootstrap" "github.com/spacemeshos/go-spacemesh/common/types" - "github.com/spacemeshos/go-spacemesh/log" ) const fileRegex = "/epoch-(?P[0-9]+)-update-(?P[a-z]+)" @@ -45,7 +45,7 @@ func (np *NetworkParam) updateActiveSetTime(targetEpoch types.EpochID) time.Time type Server struct { *http.Server eg errgroup.Group - logger log.Log + logger *zap.Logger fs afero.Fs gen *Generator genFallback bool @@ -55,7 +55,7 @@ type Server struct { type SrvOpt func(*Server) -func WithSrvLogger(logger log.Log) SrvOpt { +func WithSrvLogger(logger *zap.Logger) SrvOpt { return func(s *Server) { s.logger = logger } @@ -76,7 +76,7 @@ func WithBootstrapEpochs(epochs []types.EpochID) SrvOpt { func NewServer(gen *Generator, fallback bool, port int, opts ...SrvOpt) *Server { s := &Server{ Server: &http.Server{Addr: fmt.Sprintf(":%d", port)}, - logger: log.NewNop(), + logger: zap.NewNop(), fs: afero.NewOsFs(), gen: gen, genFallback: fallback, @@ -102,7 +102,7 @@ func (s *Server) Start(ctx context.Context, errCh chan error, params *NetworkPar http.HandleFunc("/", s.handle) http.HandleFunc("/checkpoint", s.handleCheckpoint) http.HandleFunc("/updateCheckpoint", s.handleUpdate) - s.logger.With().Info("server starts serving", log.String("addr", ln.Addr().String())) + s.logger.Info("server starts serving", zap.Stringer("addr", ln.Addr())) if err = s.Serve(ln); err != nil { errCh <- err return err @@ -170,7 +170,7 @@ func (s *Server) genWithRetry(ctx context.Context, epoch types.EpochID, maxRetri if err == nil { return nil } - s.logger.With().Debug("generate fallback active set retry", log.Err(err)) + s.logger.Debug("generate fallback active set retry", zap.Error(err)) retries := 0 backoff := 10 * time.Second @@ -180,7 +180,7 @@ func (s *Server) genWithRetry(ctx context.Context, epoch types.EpochID, maxRetri select { case <-timer.C: if err := s.GenFallbackActiveSet(ctx, epoch); err != nil { - s.logger.With().Debug("generate fallback active set retry", log.Err(err)) + s.logger.Debug("generate fallback active set retry", zap.Error(err)) retries++ if retries >= maxRetries { return err @@ -245,7 +245,7 @@ func getPartialActiveSet(ctx context.Context, smEndpoint string, targetEpoch typ } func (s *Server) Stop(ctx context.Context) { - s.logger.With().Info("shutting down server") + s.logger.Info("shutting down server") s.Shutdown(ctx) s.eg.Wait() } @@ -253,13 +253,13 @@ func (s *Server) Stop(ctx context.Context) { func (s *Server) handle(w http.ResponseWriter, r *http.Request) { matches := s.regex.FindStringSubmatch(r.URL.String()) if len(matches) != 3 { - s.logger.With().Error("unrecognized url", log.String("url", r.URL.String())) + s.logger.Error("unrecognized url", zap.Stringer("url", r.URL)) w.WriteHeader(http.StatusNotFound) return } e, err := strconv.Atoi(matches[1]) if err != nil { - s.logger.With().Error("unrecognized url", log.String("url", r.URL.String()), log.Err(err)) + s.logger.Error("unrecognized url", zap.Stringer("url", r.URL), zap.Error(err)) w.WriteHeader(http.StatusNotFound) return } @@ -310,8 +310,5 @@ func (s *Server) handleUpdate(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "save checkpoint err: %v", err) return } - s.logger.With().Info("saved checkpoint data", - log.String("data", data), - log.String("filename", filename), - ) + s.logger.Info("saved checkpoint data", zap.String("data", data), zap.String("filename", filename)) } diff --git a/cmd/bootstrapper/server_test.go b/cmd/bootstrapper/server_test.go index 910d448c79..1bdb61c6bb 100644 --- a/cmd/bootstrapper/server_test.go +++ b/cmd/bootstrapper/server_test.go @@ -19,7 +19,6 @@ import ( "github.com/spacemeshos/go-spacemesh/bootstrap" "github.com/spacemeshos/go-spacemesh/common/types" "github.com/spacemeshos/go-spacemesh/datastore" - "github.com/spacemeshos/go-spacemesh/log/logtest" "github.com/spacemeshos/go-spacemesh/sql" ) @@ -65,14 +64,14 @@ func TestServer(t *testing.T) { g := NewGenerator( "", cfg.PublicListener, - WithLogger(logtest.New(t)), + WithLogger(zaptest.NewLogger(t)), WithFilesystem(fs), ) epochs := []types.EpochID{types.EpochID(4), types.EpochID(5)} srv := NewServer(g, false, port, WithSrvFilesystem(fs), - WithSrvLogger(logtest.New(t)), + WithSrvLogger(zaptest.NewLogger(t)), WithBootstrapEpochs(epochs), ) np := &NetworkParam{