Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slog #131

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions chains/tendermint/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"cosmossdk.io/errors"
"github.com/avast/retry-go"
"github.com/cometbft/cometbft/libs/log"

rpcclient "github.com/cometbft/cometbft/rpc/client"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
Expand All @@ -33,6 +33,7 @@ import (
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"

"github.com/hyperledger-labs/yui-relayer/core"
"github.com/hyperledger-labs/yui-relayer/log"
)

var (
Expand All @@ -55,7 +56,6 @@ type Chain struct {
codec codec.ProtoCodecMarshaler `yaml:"-" json:"-"`
msgEventListener core.MsgEventListener

logger log.Logger
timeout time.Duration
debug bool

Expand Down Expand Up @@ -132,7 +132,6 @@ func (c *Chain) Init(homePath string, timeout time.Duration, codec codec.ProtoCo
c.Client = client
c.HomePath = homePath
c.codec = codec
c.logger = defaultChainLogger()
c.timeout = timeout
c.debug = debug
c.faucetAddrs = make(map[string]time.Time)
Expand Down Expand Up @@ -174,6 +173,7 @@ func (c *Chain) RegisterMsgEventListener(listener core.MsgEventListener) {
}

func (c *Chain) sendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, error) {
logger := GetChainLogger()
// broadcast tx
res, _, err := c.rawSendMsgs(msgs)
if err != nil {
Expand All @@ -194,7 +194,7 @@ func (c *Chain) sendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, error) {
// call msgEventListener if needed
if c.msgEventListener != nil {
if err := c.msgEventListener.OnSentMsg(msgs); err != nil {
c.logger.Error("failed to OnSendMsg call", "msgs", msgs, "err", err)
logger.Error("failed to OnSendMsg call", err)
return res, nil
}
}
Expand Down Expand Up @@ -567,8 +567,9 @@ func newRPCClient(addr string, timeout time.Duration) (*rpchttp.HTTP, error) {
return rpcClient, nil
}

func defaultChainLogger() log.Logger {
return log.NewTMLogger(log.NewSyncWriter(os.Stdout))
func GetChainLogger() *log.RelayLogger {
return log.GetLogger().
WithModule("tendermint.chain")
}

// CreateMnemonic creates a new mnemonic
Expand Down
18 changes: 7 additions & 11 deletions chains/tendermint/log-chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,35 @@ import (

// LogFailedTx takes the transaction and the messages to create it and logs the appropriate data
func (c *Chain) LogFailedTx(res *sdk.TxResponse, err error, msgs []sdk.Msg) {
logger := GetChainLogger()
if c.debug {
c.Log(fmt.Sprintf("- [%s] -> sending transaction:", c.ChainID()))
logger.Info("sending-tx", "chain-id", c.ChainID())
for _, msg := range msgs {
c.Print(msg, false, false)
}
}

if err != nil {
c.logger.Error(fmt.Errorf("- [%s] -> err(%v)", c.ChainID(), err).Error())
logger.Error("failed-tx", err, "chain-id", c.ChainID())
if res == nil {
return
}
}

if res.Code != 0 && res.Codespace != "" {
c.logger.Info(fmt.Sprintf("✘ [%s]@{%d} - msg(%s) err(%s:%d:%s)",
c.ChainID(), res.Height, getMsgAction(msgs), res.Codespace, res.Code, res.RawLog))
logger.Info("res", "chain-id", c.ChainID(), "height", res.Height, "action", getMsgAction(msgs), "codespace", res.Codespace, "code", res.Code, "raw-log", res.RawLog)
}

if c.debug && !res.Empty() {
c.Log("- transaction response:")
logger.Info("tx-response", "chain-id", c.ChainID(), "res", res)
c.Print(res, false, false)
}
}

// LogSuccessTx take the transaction and the messages to create it and logs the appropriate data
func (c *Chain) LogSuccessTx(res *sdk.TxResponse, msgs []sdk.Msg) {
c.logger.Info(fmt.Sprintf("✔ [%s]@{%d} - msg(%s) hash(%s)", c.ChainID(), res.Height, getMsgAction(msgs), res.TxHash))
}

// Log takes a string and logs the data
func (c *Chain) Log(s string) {
c.logger.Info(s)
logger := GetChainLogger()
logger.Info("success-tx", "chain-id", c.ChainID(), "height", res.Height, "hash", res.TxHash)
}

// Print fmt.Printlns the json or yaml representation of whatever is passed in
Expand Down
Loading