From 1d9e3dc575a37a66840f51cfbc3c192ad2b8d462 Mon Sep 17 00:00:00 2001 From: jingwei zhuang Date: Thu, 8 Dec 2022 15:03:33 -0800 Subject: [PATCH 1/5] change fmt to log --- pkg/logger/logger.go | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 72e79047..8af410d1 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -168,7 +168,7 @@ func (l *Logger) LogDataStatus(ctx context.Context, status *results.CheckDataSta } l.lastStatsMessage = statsMessage - color.Cyan(statsMessage) + l.zapLogger.Info(statsMessage) // If Progress is nil, it means we're already done. if status.Progress == nil { @@ -194,7 +194,7 @@ func (l *Logger) LogDataStatus(ctx context.Context, status *results.CheckDataSta } l.lastProgressMessage = progressMessage - color.Cyan(progressMessage) + l.zapLogger.Info(progressMessage) } // LogConstructionStatus logs results.CheckConstructionStatus. @@ -218,7 +218,7 @@ func (l *Logger) LogConstructionStatus( statsMessage = AddRequestUUID(statsMessage, l.logRequestUUID) l.lastStatsMessage = statsMessage - color.Cyan(statsMessage) + l.zapLogger.Info(statsMessage) } // LogMemoryStats logs memory usage information. @@ -257,7 +257,7 @@ func (l *Logger) AddBlockStream( defer closeFile(f) blockString := fmt.Sprintf( - "%s Block %d:%s with Parent Block %d:%s, RequestUUID: %s\n", + "%s Block %d:%s with Parent Block %d:%s, RequestUUID: %s", addEvent, block.BlockIdentifier.Index, block.BlockIdentifier.Hash, @@ -265,7 +265,7 @@ func (l *Logger) AddBlockStream( block.ParentBlockIdentifier.Hash, l.logRequestUUID, ) - fmt.Print(blockString) + l.zapLogger.Info(blockString) if _, err := f.WriteString(blockString); err != nil { return fmt.Errorf("failed to write block string %s: %w", blockString, err) } @@ -295,13 +295,13 @@ func (l *Logger) RemoveBlockStream( defer closeFile(f) blockString := fmt.Sprintf( - "%s Block %d:%s, RequestUUID: %s\n", + "%s Block %d:%s, RequestUUID: %s", removeEvent, block.Index, block.Hash, l.logRequestUUID, ) - fmt.Print(blockString) + l.zapLogger.Info(blockString) _, err = f.WriteString(blockString) if err != nil { return fmt.Errorf("failed to write block string %s: %w", blockString, err) @@ -333,13 +333,13 @@ func (l *Logger) TransactionStream( for _, tx := range block.Transactions { transactionString := fmt.Sprintf( - "Transaction %s at Block %d:%s, RequestUUID: %s\n", + "Transaction %s at Block %d:%s, RequestUUID: %s", tx.TransactionIdentifier.Hash, block.BlockIdentifier.Index, block.BlockIdentifier.Hash, l.logRequestUUID, ) - fmt.Print(transactionString) + l.zapLogger.Info(transactionString) _, err = f.WriteString(transactionString) if err != nil { return fmt.Errorf("failed to write transaction string %s: %w", transactionString, err) @@ -363,7 +363,7 @@ func (l *Logger) TransactionStream( } transactionOperationString := fmt.Sprintf( - "TxOp %d(%d) %s %s %s %s %s\n", + "TxOp %d(%d) %s %s %s %s %s, RequestUUID: %s", op.OperationIdentifier.Index, networkIndex, op.Type, @@ -371,7 +371,9 @@ func (l *Logger) TransactionStream( amount, symbol, *op.Status, + l.logRequestUUID, ) + l.zapLogger.Info(transactionOperationString) _, err = f.WriteString(transactionOperationString) if err != nil { return fmt.Errorf("failed to write transaction operation string %s: %w", transactionOperationString, err) @@ -405,14 +407,16 @@ func (l *Logger) BalanceStream( for _, balanceChange := range balanceChanges { balanceLog := fmt.Sprintf( - "Account: %s Change: %s:%s Block: %d:%s", + "Account: %s Change: %s:%s Block: %d:%s, RequestUUID: %s", balanceChange.Account.Address, balanceChange.Difference, types.CurrencyString(balanceChange.Currency), balanceChange.Block.Index, balanceChange.Block.Hash, + l.logRequestUUID, ) balanceLog = AddRequestUUID(balanceLog, l.logRequestUUID) + l.zapLogger.Info(balanceLog) if _, err := f.WriteString(fmt.Sprintf("%s\n", balanceLog)); err != nil { return fmt.Errorf("failed to write balance log %s: %w", balanceLog, err) } @@ -446,14 +450,15 @@ func (l *Logger) ReconcileSuccessStream( defer closeFile(f) log.Printf( - "%s Reconciled %s at %d\n", + "%s Reconciled %s at %d, RequestUUID: %s\n", reconciliationType, types.AccountString(account), block.Index, + l.logRequestUUID, ) reconciliationSuccessString := fmt.Sprintf( - "Type:%s Account: %s Currency: %s Balance: %s Block: %d:%s, RequestUUID: %s\n", + "Type:%s Account: %s Currency: %s Balance: %s Block: %d:%s, RequestUUID: %s", reconciliationType, types.AccountString(account), types.CurrencyString(currency), @@ -462,6 +467,8 @@ func (l *Logger) ReconcileSuccessStream( block.Hash, l.logRequestUUID, ) + l.zapLogger.Info(reconciliationSuccessString) + _, err = f.WriteString(reconciliationSuccessString) if err != nil { return fmt.Errorf("failed to write reconciliation success string %s: %w", reconciliationSuccessString, err) @@ -519,7 +526,7 @@ func (l *Logger) ReconcileFailureStream( defer closeFile(f) reconciliationFailureString := fmt.Sprintf( - "Type:%s Account: %s Currency: %s Block: %s:%d computed: %s live: %s, RequestUUID: %s\n", + "Type:%s Account: %s Currency: %s Block: %s:%d computed: %s live: %s, RequestUUID: %s", reconciliationType, types.AccountString(account), types.CurrencyString(currency), @@ -529,6 +536,7 @@ func (l *Logger) ReconcileFailureStream( liveBalance, l.logRequestUUID, ) + l.zapLogger.Info(reconciliationFailureString) _, err = f.WriteString(reconciliationFailureString) if err != nil { return fmt.Errorf("failed to write reconciliation failure string %s: %w", reconciliationFailureString, err) @@ -590,7 +598,7 @@ func LogTransactionCreated( func AddRequestUUIDFromContext(ctx context.Context, msg string) string { requestUUID := requestUUIDFromContext(ctx) if requestUUID != "" { - msg = fmt.Sprintf("%s,RequestUUID: %s\n", msg, requestUUID) + msg = fmt.Sprintf("%s,RequestUUID: %s", msg, requestUUID) } return msg } @@ -598,7 +606,7 @@ func AddRequestUUIDFromContext(ctx context.Context, msg string) string { // Add requestUUID to the tip func AddRequestUUID(msg string, requestUUID string) string { if requestUUID != "" { - msg = fmt.Sprintf("%s,RequestUUID: %s\n", msg, requestUUID) + msg = fmt.Sprintf("%s,RequestUUID: %s", msg, requestUUID) } return msg } From 5d662ee5eb391b1285258564a9d262f99fd5a94e Mon Sep 17 00:00:00 2001 From: jingwei zhuang Date: Thu, 8 Dec 2022 15:17:49 -0800 Subject: [PATCH 2/5] updates --- pkg/logger/logger.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 8af410d1..92922a2a 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -598,7 +598,7 @@ func LogTransactionCreated( func AddRequestUUIDFromContext(ctx context.Context, msg string) string { requestUUID := requestUUIDFromContext(ctx) if requestUUID != "" { - msg = fmt.Sprintf("%s,RequestUUID: %s", msg, requestUUID) + msg = fmt.Sprintf("%s, RequestUUID: %s", msg, requestUUID) } return msg } @@ -606,7 +606,7 @@ func AddRequestUUIDFromContext(ctx context.Context, msg string) string { // Add requestUUID to the tip func AddRequestUUID(msg string, requestUUID string) string { if requestUUID != "" { - msg = fmt.Sprintf("%s,RequestUUID: %s", msg, requestUUID) + msg = fmt.Sprintf("%s, RequestUUID: %s", msg, requestUUID) } return msg } From 3e7e85181f4547e2fc7f8323d68faff15c0f4771 Mon Sep 17 00:00:00 2001 From: jingwei zhuang Date: Thu, 8 Dec 2022 15:36:55 -0800 Subject: [PATCH 3/5] updates --- pkg/logger/logger.go | 48 +++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 92922a2a..7ee69773 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -251,7 +251,9 @@ func (l *Logger) AddBlockStream( os.FileMode(utils.DefaultFilePermissions), ) if err != nil { - return fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, blockStreamFile), err) + err = fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, blockStreamFile), err) + l.zapLogger.Error(err.Error()) + return err } defer closeFile(f) @@ -289,7 +291,9 @@ func (l *Logger) RemoveBlockStream( os.FileMode(utils.DefaultFilePermissions), ) if err != nil { - return fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, blockStreamFile), err) + err = fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, blockStreamFile), err) + l.zapLogger.Error(err.Error()) + return err } defer closeFile(f) @@ -304,7 +308,9 @@ func (l *Logger) RemoveBlockStream( l.zapLogger.Info(blockString) _, err = f.WriteString(blockString) if err != nil { - return fmt.Errorf("failed to write block string %s: %w", blockString, err) + err = fmt.Errorf("failed to write block string %s: %w", blockString, err) + l.zapLogger.Error(err.Error()) + return err } return nil @@ -326,7 +332,9 @@ func (l *Logger) TransactionStream( os.FileMode(utils.DefaultFilePermissions), ) if err != nil { - return fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, transactionStreamFile), err) + err = fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, transactionStreamFile), err) + l.zapLogger.Error(err.Error()) + return err } defer closeFile(f) @@ -342,7 +350,9 @@ func (l *Logger) TransactionStream( l.zapLogger.Info(transactionString) _, err = f.WriteString(transactionString) if err != nil { - return fmt.Errorf("failed to write transaction string %s: %w", transactionString, err) + err = fmt.Errorf("failed to write transaction string %s: %w", transactionString, err) + l.zapLogger.Error(err.Error()) + return err } for _, op := range tx.Operations { @@ -376,7 +386,9 @@ func (l *Logger) TransactionStream( l.zapLogger.Info(transactionOperationString) _, err = f.WriteString(transactionOperationString) if err != nil { - return fmt.Errorf("failed to write transaction operation string %s: %w", transactionOperationString, err) + err = fmt.Errorf("failed to write transaction operation string %s: %w", transactionOperationString, err) + l.zapLogger.Error(err.Error()) + return err } } } @@ -400,7 +412,9 @@ func (l *Logger) BalanceStream( os.FileMode(utils.DefaultFilePermissions), ) if err != nil { - return fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, balanceStreamFile), err) + err = fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, balanceStreamFile), err) + l.zapLogger.Error(err.Error()) + return err } defer closeFile(f) @@ -418,7 +432,9 @@ func (l *Logger) BalanceStream( balanceLog = AddRequestUUID(balanceLog, l.logRequestUUID) l.zapLogger.Info(balanceLog) if _, err := f.WriteString(fmt.Sprintf("%s\n", balanceLog)); err != nil { - return fmt.Errorf("failed to write balance log %s: %w", balanceLog, err) + err = fmt.Errorf("failed to write balance log %s: %w", balanceLog, err) + l.zapLogger.Error(err.Error()) + return err } } return nil @@ -444,7 +460,9 @@ func (l *Logger) ReconcileSuccessStream( os.FileMode(utils.DefaultFilePermissions), ) if err != nil { - return fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, reconcileSuccessStreamFile), err) + err = fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, reconcileSuccessStreamFile), err) + l.zapLogger.Error(err.Error()) + return err } defer closeFile(f) @@ -471,7 +489,9 @@ func (l *Logger) ReconcileSuccessStream( _, err = f.WriteString(reconciliationSuccessString) if err != nil { - return fmt.Errorf("failed to write reconciliation success string %s: %w", reconciliationSuccessString, err) + err = fmt.Errorf("failed to write reconciliation success string %s: %w", reconciliationSuccessString, err) + l.zapLogger.Error(err.Error()) + return err } return nil @@ -520,7 +540,9 @@ func (l *Logger) ReconcileFailureStream( os.FileMode(utils.DefaultFilePermissions), ) if err != nil { - return fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, reconcileFailureStreamFile), err) + err = fmt.Errorf("failed to open file %s: %w", path.Join(l.logDir, reconcileFailureStreamFile), err) + l.zapLogger.Error(err.Error()) + return err } defer closeFile(f) @@ -539,7 +561,9 @@ func (l *Logger) ReconcileFailureStream( l.zapLogger.Info(reconciliationFailureString) _, err = f.WriteString(reconciliationFailureString) if err != nil { - return fmt.Errorf("failed to write reconciliation failure string %s: %w", reconciliationFailureString, err) + err = fmt.Errorf("failed to write reconciliation failure string %s: %w", reconciliationFailureString, err) + l.zapLogger.Error(err.Error()) + return err } return nil From 42bec40e8bcf462a5420d72be911ef392343ecfb Mon Sep 17 00:00:00 2001 From: jingwei zhuang Date: Thu, 8 Dec 2022 16:42:42 -0800 Subject: [PATCH 4/5] updates --- pkg/logger/logger.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 7ee69773..d758e6c5 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -124,7 +124,7 @@ func buildZapLogger( fields ...zap.Field, ) (*zap.Logger, error) { config := zap.NewProductionConfig() - config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder + config.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder baseSlice := []zap.Field{ zap.String("blockchain", network.Blockchain), From b95752f7ac1bdc5a3d9e8e0d46b0b428e203831a Mon Sep 17 00:00:00 2001 From: jingwei zhuang Date: Thu, 8 Dec 2022 17:19:19 -0800 Subject: [PATCH 5/5] updates --- pkg/logger/logger.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index d758e6c5..cd2aebe4 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -259,14 +259,14 @@ func (l *Logger) AddBlockStream( defer closeFile(f) blockString := fmt.Sprintf( - "%s Block %d:%s with Parent Block %d:%s, RequestUUID: %s", + "%s Block %d:%s with Parent Block %d:%s", addEvent, block.BlockIdentifier.Index, block.BlockIdentifier.Hash, block.ParentBlockIdentifier.Index, block.ParentBlockIdentifier.Hash, - l.logRequestUUID, ) + blockString = AddRequestUUID(blockString, l.logRequestUUID) l.zapLogger.Info(blockString) if _, err := f.WriteString(blockString); err != nil { return fmt.Errorf("failed to write block string %s: %w", blockString, err) @@ -299,12 +299,12 @@ func (l *Logger) RemoveBlockStream( defer closeFile(f) blockString := fmt.Sprintf( - "%s Block %d:%s, RequestUUID: %s", + "%s Block %d:%s", removeEvent, block.Index, block.Hash, - l.logRequestUUID, ) + blockString = AddRequestUUID(blockString, l.logRequestUUID) l.zapLogger.Info(blockString) _, err = f.WriteString(blockString) if err != nil { @@ -341,12 +341,12 @@ func (l *Logger) TransactionStream( for _, tx := range block.Transactions { transactionString := fmt.Sprintf( - "Transaction %s at Block %d:%s, RequestUUID: %s", + "Transaction %s at Block %d:%s", tx.TransactionIdentifier.Hash, block.BlockIdentifier.Index, block.BlockIdentifier.Hash, - l.logRequestUUID, ) + transactionString = AddRequestUUID(transactionString, l.logRequestUUID) l.zapLogger.Info(transactionString) _, err = f.WriteString(transactionString) if err != nil { @@ -373,7 +373,7 @@ func (l *Logger) TransactionStream( } transactionOperationString := fmt.Sprintf( - "TxOp %d(%d) %s %s %s %s %s, RequestUUID: %s", + "TxOp %d(%d) %s %s %s %s %s", op.OperationIdentifier.Index, networkIndex, op.Type, @@ -381,8 +381,8 @@ func (l *Logger) TransactionStream( amount, symbol, *op.Status, - l.logRequestUUID, ) + transactionOperationString = AddRequestUUID(transactionOperationString, l.logRequestUUID) l.zapLogger.Info(transactionOperationString) _, err = f.WriteString(transactionOperationString) if err != nil { @@ -421,13 +421,12 @@ func (l *Logger) BalanceStream( for _, balanceChange := range balanceChanges { balanceLog := fmt.Sprintf( - "Account: %s Change: %s:%s Block: %d:%s, RequestUUID: %s", + "Account: %s Change: %s:%s Block: %d:%s", balanceChange.Account.Address, balanceChange.Difference, types.CurrencyString(balanceChange.Currency), balanceChange.Block.Index, balanceChange.Block.Hash, - l.logRequestUUID, ) balanceLog = AddRequestUUID(balanceLog, l.logRequestUUID) l.zapLogger.Info(balanceLog) @@ -467,24 +466,25 @@ func (l *Logger) ReconcileSuccessStream( defer closeFile(f) - log.Printf( - "%s Reconciled %s at %d, RequestUUID: %s\n", + reconciledLog := fmt.Sprintf( + "%s Reconciled %s at %d", reconciliationType, types.AccountString(account), block.Index, - l.logRequestUUID, ) + reconciledLog = AddRequestUUID(reconciledLog, l.logRequestUUID) + l.zapLogger.Info(reconciledLog) reconciliationSuccessString := fmt.Sprintf( - "Type:%s Account: %s Currency: %s Balance: %s Block: %d:%s, RequestUUID: %s", + "Type:%s Account: %s Currency: %s Balance: %s Block: %d:%s", reconciliationType, types.AccountString(account), types.CurrencyString(currency), balance, block.Index, block.Hash, - l.logRequestUUID, ) + reconciliationSuccessString = AddRequestUUID(reconciliationSuccessString, l.logRequestUUID) l.zapLogger.Info(reconciliationSuccessString) _, err = f.WriteString(reconciliationSuccessString) @@ -548,7 +548,7 @@ func (l *Logger) ReconcileFailureStream( defer closeFile(f) reconciliationFailureString := fmt.Sprintf( - "Type:%s Account: %s Currency: %s Block: %s:%d computed: %s live: %s, RequestUUID: %s", + "Type:%s Account: %s Currency: %s Block: %s:%d computed: %s live: %s", reconciliationType, types.AccountString(account), types.CurrencyString(currency), @@ -556,8 +556,8 @@ func (l *Logger) ReconcileFailureStream( block.Index, computedBalance, liveBalance, - l.logRequestUUID, ) + reconciliationFailureString = AddRequestUUID(reconciliationFailureString, l.logRequestUUID) l.zapLogger.Info(reconciliationFailureString) _, err = f.WriteString(reconciliationFailureString) if err != nil {