Skip to content

Commit

Permalink
Refactoring: better logging lib (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Jul 6, 2021
1 parent 8fd5200 commit 1d17dd8
Show file tree
Hide file tree
Showing 83 changed files with 357 additions and 397 deletions.
2 changes: 1 addition & 1 deletion cmd/api/handlers/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (ctx *Context) handleError(c *gin.Context, err error, code int) bool {
if hub := sentrygin.GetHubFromContext(c); hub != nil {
hub.CaptureMessage(err.Error())
}
logger.Error(err)
logger.Err(err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type app struct {
func newApp() *app {
cfg, err := config.LoadDefaultConfig()
if err != nil {
logger.Fatal(err)
panic(err)
}

docs.SwaggerInfo.Host = cfg.API.SwaggerHost
Expand All @@ -40,7 +40,7 @@ func newApp() *app {

ctx, err := handlers.NewContext(cfg)
if err != nil {
logger.Error(err)
logger.Err(err)
helpers.CatchErrorSentry(err)
return nil
}
Expand All @@ -62,7 +62,7 @@ func (api *app) makeRouter() {

if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
if err := validations.Register(v, api.Context.Config.API); err != nil {
logger.Fatal(err)
panic(err)
}
}

Expand Down Expand Up @@ -218,7 +218,7 @@ func (api *app) Close() {

func (api *app) Run() {
if err := api.Router.Run(api.Context.Config.API.Bind); err != nil {
logger.Error(err)
logger.Err(err)
helpers.CatchErrorSentry(err)
return
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/graphql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/logger"
"github.com/dosco/graphjin/core"
"github.com/gin-gonic/gin"
_ "github.com/jackc/pgx/v4/stdlib"
Expand Down Expand Up @@ -75,16 +74,16 @@ func initUser() error {
func main() {
cfg, err := config.LoadDefaultConfig()
if err != nil {
logger.Fatal(err)
panic(err)
}

if err := initUser(); err != nil {
logger.Fatal(err)
panic(err)
}

db, err := sql.Open("pgx", cfg.GraphQL.DB)
if err != nil {
logger.Fatal(err)
panic(err)
}
defer db.Close()

Expand All @@ -98,7 +97,7 @@ func main() {
Debug: true,
}, db)
if err != nil {
logger.Fatal(err)
panic(err)
}

ctx := apiContext{
Expand Down
50 changes: 25 additions & 25 deletions cmd/indexer/indexer/boost.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type BoostIndexer struct {
}

func (bi *BoostIndexer) fetchExternalProtocols() error {
logger.WithNetwork(bi.Network).Info("Fetching external protocols")
logger.Info().Str("network", bi.Network.String()).Msg("Fetching external protocols")
existingProtocols, err := bi.Protocols.GetByNetworkWithSort(bi.Network, "start_level", "desc")
if err != nil {
return err
Expand Down Expand Up @@ -94,15 +94,15 @@ func (bi *BoostIndexer) fetchExternalProtocols() error {
}

protocols = append(protocols, newProtocol)
logger.WithNetwork(bi.Network).Infof("Fetched %s", alias)
logger.Info().Str("network", bi.Network.String()).Msgf("Fetched %s", alias)
}

return bi.Storage.Save(protocols)
}

// NewBoostIndexer -
func NewBoostIndexer(cfg config.Config, network types.Network, opts ...BoostIndexerOption) (*BoostIndexer, error) {
logger.WithNetwork(network).Info("Creating indexer object...")
logger.Info().Str("network", network.String()).Msg("Creating indexer object...")

rpcProvider, ok := cfg.RPC[network.String()]
if !ok {
Expand Down Expand Up @@ -184,7 +184,7 @@ func (bi *BoostIndexer) init(db *core.Postgres) error {
return err
}
bi.state = currentState
logger.WithNetwork(bi.Network).Infof("Current indexer state: %d", currentState.Level)
logger.Info().Str("network", bi.Network.String()).Msgf("Current indexer state: %d", currentState.Level)

currentProtocol, err := bi.Protocols.Get(bi.Network, "", currentState.Level)
if err != nil {
Expand All @@ -207,7 +207,7 @@ func (bi *BoostIndexer) init(db *core.Postgres) error {
}

bi.currentProtocol = currentProtocol
logger.WithNetwork(bi.Network).Infof("Current network protocol: %s", currentProtocol.Hash)
logger.Info().Str("network", bi.Network.String()).Msgf("Current network protocol: %s", currentProtocol.Hash)
return nil
}

Expand All @@ -221,7 +221,7 @@ func (bi *BoostIndexer) Sync(wg *sync.WaitGroup) {

// First tick
if err := bi.process(); err != nil {
logger.Error(err)
logger.Err(err)
helpers.CatchErrorSentry(err)
}
if bi.stopped {
Expand Down Expand Up @@ -249,7 +249,7 @@ func (bi *BoostIndexer) Sync(wg *sync.WaitGroup) {
}
continue
}
logger.Error(err)
logger.Err(err)
helpers.CatchErrorSentry(err)
}

Expand All @@ -274,7 +274,7 @@ func (bi *BoostIndexer) setUpdateTicker(seconds int) {
} else {
duration = time.Duration(seconds) * time.Second
}
logger.WithNetwork(bi.Network).Infof("Data will be updated every %.0f seconds", duration.Seconds())
logger.Info().Str("network", bi.Network.String()).Msgf("Data will be updated every %.0f seconds", duration.Seconds())
bi.updateTicker = time.NewTicker(duration)
}

Expand Down Expand Up @@ -322,10 +322,10 @@ func (bi *BoostIndexer) handleBlock(head noderpc.Header) error {
result := parsers.NewResult()
err := bi.StorageDB.DB.Transaction(
func(tx *gorm.DB) error {
logger.WithNetwork(bi.Network).Infof("indexing %d block", head.Level)
logger.Info().Str("network", bi.Network.String()).Msgf("indexing %d block", head.Level)

if head.Protocol != bi.currentProtocol.Hash {
logger.WithNetwork(bi.Network).Infof("New protocol detected: %s -> %s", bi.currentProtocol.Hash, head.Protocol)
logger.Info().Str("network", bi.Network.String()).Msgf("New protocol detected: %s -> %s", bi.currentProtocol.Hash, head.Protocol)

if err := bi.migrate(head, tx); err != nil {
return err
Expand Down Expand Up @@ -358,7 +358,7 @@ func (bi *BoostIndexer) handleBlock(head noderpc.Header) error {

// Rollback -
func (bi *BoostIndexer) Rollback() error {
logger.WithNetwork(bi.Network).Warningf("Rollback from %d", bi.state.Level)
logger.Warning().Str("network", bi.Network.String()).Msgf("Rollback from %d", bi.state.Level)

lastLevel, err := bi.getLastRollbackBlock()
if err != nil {
Expand All @@ -377,8 +377,8 @@ func (bi *BoostIndexer) Rollback() error {
return err
}
bi.state = newState
logger.WithNetwork(bi.Network).Infof("New indexer state: %d", bi.state.Level)
logger.WithNetwork(bi.Network).Info("Rollback finished")
logger.Info().Str("network", bi.Network.String()).Msgf("New indexer state: %d", bi.state.Level)
logger.Info().Str("network", bi.Network.String()).Msg("Rollback finished")
return nil
}

Expand All @@ -398,7 +398,7 @@ func (bi *BoostIndexer) getLastRollbackBlock() (int64, error) {
}

if block.Predecessor == headAtLevel.Predecessor {
logger.WithNetwork(bi.Network).Warnf("Found equal predecessors at level: %d", block.Level)
logger.Warning().Str("network", bi.Network.String()).Msgf("Found equal predecessors at level: %d", block.Level)
end = true
lastLevel = block.Level - 1
}
Expand Down Expand Up @@ -438,8 +438,8 @@ func (bi *BoostIndexer) process() error {
return errors.Errorf("Invalid chain_id: %s (state) != %s (head)", bi.state.ChainID, head.ChainID)
}

logger.WithNetwork(bi.Network).Infof("Current node state: %d", head.Level)
logger.WithNetwork(bi.Network).Infof("Current indexer state: %d", bi.state.Level)
logger.Info().Str("network", bi.Network.String()).Msgf("Current node state: %d", head.Level)
logger.Info().Str("network", bi.Network.String()).Msgf("Current indexer state: %d", bi.state.Level)

if head.Level > bi.state.Level {
levels := make([]int64, 0)
Expand All @@ -454,7 +454,7 @@ func (bi *BoostIndexer) process() error {
}
}

logger.WithNetwork(bi.Network).Infof("Found %d new levels", len(levels))
logger.Info().Str("network", bi.Network.String()).Msgf("Found %d new levels", len(levels))

if err := bi.Index(levels); err != nil {
if errors.Is(err, errBcdQuit) {
Expand All @@ -474,7 +474,7 @@ func (bi *BoostIndexer) process() error {
if bi.boost {
bi.boost = false
}
logger.WithNetwork(bi.Network).Info("Synced")
logger.Info().Str("network", bi.Network.String()).Msg("Synced")
return nil
} else if head.Level < bi.state.Level {
if err := bi.Rollback(); err != nil {
Expand Down Expand Up @@ -561,7 +561,7 @@ func (bi *BoostIndexer) getDataFromBlock(head noderpc.Header) (*parsers.Result,

func (bi *BoostIndexer) migrate(head noderpc.Header, tx *gorm.DB) error {
if bi.currentProtocol.EndLevel == 0 && head.Level > 1 {
logger.WithNetwork(bi.Network).Infof("Finalizing the previous protocol: %s", bi.currentProtocol.Alias)
logger.Info().Str("network", bi.Network.String()).Msgf("Finalizing the previous protocol: %s", bi.currentProtocol.Alias)
bi.currentProtocol.EndLevel = head.Level - 1
if err := bi.currentProtocol.Save(bi.StorageDB.DB); err != nil {
return err
Expand All @@ -570,7 +570,7 @@ func (bi *BoostIndexer) migrate(head noderpc.Header, tx *gorm.DB) error {

newProtocol, err := bi.Protocols.Get(bi.Network, head.Protocol, head.Level)
if err != nil {
logger.Warning("%s", err)
logger.Warning().Str("network", bi.Network.String()).Msgf("%s", err)
newProtocol, err = createProtocol(bi.rpc, bi.Network, head.Protocol, head.Level)
if err != nil {
return err
Expand All @@ -593,32 +593,32 @@ func (bi *BoostIndexer) migrate(head noderpc.Header, tx *gorm.DB) error {
return err
}
} else {
logger.WithNetwork(bi.Network).Infof("Same symlink %s for %s / %s",
logger.Info().Str("network", bi.Network.String()).Msgf("Same symlink %s for %s / %s",
newProtocol.SymLink, bi.currentProtocol.Alias, newProtocol.Alias)
}
}

bi.currentProtocol = newProtocol

bi.setUpdateTicker(0)
logger.WithNetwork(bi.Network).Infof("Migration to %s is completed", bi.currentProtocol.Alias)
logger.Info().Str("network", bi.Network.String()).Msgf("Migration to %s is completed", bi.currentProtocol.Alias)
return nil
}

func (bi *BoostIndexer) standartMigration(newProtocol protocol.Protocol, head noderpc.Header, tx *gorm.DB) error {
logger.WithNetwork(bi.Network).Info("Try to find migrations...")
logger.Info().Str("network", bi.Network.String()).Msg("Try to find migrations...")
contracts, err := bi.Contracts.GetMany(map[string]interface{}{
"network": bi.Network,
})
if err != nil {
return err
}
logger.WithNetwork(bi.Network).Infof("Now %d contracts are indexed", len(contracts))
logger.Info().Str("network", bi.Network.String()).Msgf("Now %d contracts are indexed", len(contracts))

p := migrations.NewMigrationParser(bi.Storage, bi.BigMapDiffs, bi.Config.SharePath)

for i := range contracts {
logger.WithNetwork(bi.Network).Infof("Migrate %s...", contracts[i].Address)
logger.Info().Str("network", bi.Network.String()).Msgf("Migrate %s...", contracts[i].Address)
script, err := bi.rpc.GetScriptJSON(contracts[i].Address, newProtocol.StartLevel)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/indexer/indexer/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func createProtocol(rpc noderpc.INode, network types.Network, hash string, level int64) (protocol protocol.Protocol, err error) {
logger.WithNetwork(network).Infof("Creating new protocol %s starting at %d", hash, level)
logger.Info().Str("network", network.String()).Msgf("Creating new protocol %s starting at %d", hash, level)
protocol.SymLink, err = bcd.GetProtoSymLink(hash)
if err != nil {
return
Expand Down
9 changes: 5 additions & 4 deletions cmd/indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
func main() {
cfg, err := config.LoadDefaultConfig()
if err != nil {
logger.Fatal(err)
logger.Err(err)
return
}

if cfg.Indexer.SentryEnabled {
Expand All @@ -27,7 +28,7 @@ func main() {

indexers, err := indexer.CreateIndexers(cfg)
if err != nil {
logger.Error(err)
logger.Err(err)
helpers.CatchErrorSentry(err)
return
}
Expand All @@ -36,7 +37,7 @@ func main() {
if countCPU > len(indexers)+1 {
countCPU = len(indexers) + 1
}
logger.Warning("Indexer started on %d CPU cores", countCPU)
logger.Warning().Msgf("Indexer started on %d CPU cores", countCPU)
runtime.GOMAXPROCS(countCPU)

sigChan := make(chan os.Signal, 1)
Expand All @@ -54,5 +55,5 @@ func main() {
go indexers[i].Stop()
}
wg.Wait()
logger.Info("Stopped")
logger.Info().Msg("Stopped")
}
2 changes: 1 addition & 1 deletion cmd/metrics/bigmapdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func getBigMapDiff(ids []int64) error {
items = append(items, res...)
}

logger.WithField("models", len(items)).Infof("%2d big map diff processed", len(bmd))
logger.Info().Int("models", len(items)).Msgf("%2d big map diff processed", len(bmd))

if len(items) > 0 {
if err := ctx.Storage.Save(items); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/metrics/bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ func (bm *BulkManager) process(force bool) bool {
for i := range bm.queue {
id, err := parseID(bm.queue[i].Body)
if err != nil {
logger.Error(err)
logger.Err(err)
continue
}
ids[i] = id
}
if err := bm.handler(ids); err != nil {
logger.Error(err)
logger.Err(err)
return false
}
for i := range bm.queue {
if err := bm.queue[i].Ack(false); err != nil {
logger.Errorf("Error acknowledging message: %s", err)
logger.Error().Msgf("Error acknowledging message: %s", err)
return false
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/metrics/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func getContract(ids []int64) error {
updates = append(updates, res...)
}

logger.Info("%2d contracts are processed", len(contracts))
logger.Info().Msgf("%2d contracts are processed", len(contracts))

if err := saveSearchModels(ctx.Searcher, updates); err != nil {
return err
Expand Down
Loading

0 comments on commit 1d17dd8

Please sign in to comment.