From 96cdd552e7428bf7e9a967c57aa53889aa34db33 Mon Sep 17 00:00:00 2001 From: Kai Lee Date: Thu, 27 Jun 2019 11:19:03 -0700 Subject: [PATCH] Replaced loggers in `internal/` with custom shared logger --- internal/hmyapi/private_account.go | 5 +++-- internal/hmyapi/util.go | 7 ++++--- internal/utils/utils.go | 16 +++++++++------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/internal/hmyapi/private_account.go b/internal/hmyapi/private_account.go index 2620cf8f4..2ebe5c18f 100644 --- a/internal/hmyapi/private_account.go +++ b/internal/hmyapi/private_account.go @@ -4,10 +4,11 @@ import ( "context" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/log" + "github.com/harmony-one/harmony/accounts" "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/hmy" + "github.com/harmony-one/harmony/internal/utils" ) // PrivateAccountAPI provides an API to access accounts managed by this node. @@ -37,7 +38,7 @@ func (s *PrivateAccountAPI) SendTransaction(ctx context.Context, args SendTxArgs } signed, err := s.signTransaction(ctx, &args, passwd) if err != nil { - log.Warn("Failed transaction send attempt", "from", args.From, "to", args.To, "value", args.Value.ToInt(), "err", err) + utils.GetLogger().Warn("Failed transaction send attempt", "from", args.From, "to", args.To, "value", args.Value.ToInt(), "err", err) return common.Hash{}, err } return SubmitTransaction(ctx, s.b, signed) diff --git a/internal/hmyapi/util.go b/internal/hmyapi/util.go index e75cbfb32..68466ecf3 100644 --- a/internal/hmyapi/util.go +++ b/internal/hmyapi/util.go @@ -5,9 +5,10 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/log" + "github.com/harmony-one/harmony/core/types" common2 "github.com/harmony-one/harmony/internal/common" + "github.com/harmony-one/harmony/internal/utils" ) // SubmitTransaction is a helper function that submits tx to txPool and logs a message. @@ -22,9 +23,9 @@ func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c return common.Hash{}, err } addr := crypto.CreateAddress(from, tx.Nonce()) - log.Info("Submitted contract creation", "fullhash", tx.Hash().Hex(), "contract", common2.MustAddressToBech32(addr)) + utils.GetLogger().Info("Submitted contract creation", "fullhash", tx.Hash().Hex(), "contract", common2.MustAddressToBech32(addr)) } else { - log.Info("Submitted transaction", "fullhash", tx.Hash().Hex(), "recipient", tx.To()) + utils.GetLogger().Info("Submitted transaction", "fullhash", tx.Hash().Hex(), "recipient", tx.To()) } return tx.Hash(), nil } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index b095b82e5..1ec19e6dc 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "log" mrand "math/rand" "net" "os" @@ -64,7 +63,8 @@ func ConvertFixedDataIntoByteArray(data interface{}) []byte { buff := new(bytes.Buffer) err := binary.Write(buff, binary.BigEndian, data) if err != nil { - log.Panic(err) + GetLogger().Crit("Failed to convert fixed data into byte array", "err", err) + panic(err) } return buff.Bytes() } @@ -73,7 +73,8 @@ func ConvertFixedDataIntoByteArray(data interface{}) []byte { func GetUniqueIDFromIPPort(ip, port string) uint32 { reg, err := regexp.Compile("[^0-9]+") if err != nil { - log.Panic("Regex Compilation Failed", "err", err) + GetLogger().Crit("Regex Compilation Failed", "err", err) + panic(err) } socketID := reg.ReplaceAllString(ip+port, "") // A integer Id formed by unique IP/PORT pair value, _ := strconv.Atoi(socketID) @@ -199,15 +200,16 @@ func LoadKeyFromFile(keyfile string) (key p2p_crypto.PrivKey, pk p2p_crypto.PubK var keyStruct PrivKeyStore err = Load(keyfile, &keyStruct) if err != nil { - log.Print("No priviate key can be loaded from file", "keyfile", keyfile) - log.Print("Using random private key") + GetLogger().Info("No priviate key can be loaded from file", "keyfile", keyfile) + GetLogger().Info("Using random private key") key, pk, err = GenKeyP2PRand() if err != nil { - log.Panic("LoadKeyFromFile", "GenKeyP2PRand Error", err) + GetLogger().Crit("LoadKeyFromFile", "GenKeyP2PRand Error", err) + panic(err) } err = SaveKeyToFile(keyfile, key) if err != nil { - log.Print("LoadKeyFromFile", "failed to save key to keyfile", err) + GetLogger().Error("failed to save key to keyfile", "keyfile", err) } return key, pk, nil }