Skip to content

Commit f9ed49d

Browse files
authored
Merge pull request #131 from dongrie/tm-slog
Add slog
2 parents bbea0d0 + fe643c1 commit f9ed49d

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

chains/tendermint/chain.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
"cosmossdk.io/errors"
1414
"github.com/avast/retry-go"
15-
"github.com/cometbft/cometbft/libs/log"
15+
1616
rpcclient "github.com/cometbft/cometbft/rpc/client"
1717
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
1818
coretypes "github.com/cometbft/cometbft/rpc/core/types"
@@ -33,6 +33,7 @@ import (
3333
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
3434

3535
"github.com/hyperledger-labs/yui-relayer/core"
36+
"github.com/hyperledger-labs/yui-relayer/log"
3637
)
3738

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

58-
logger log.Logger
5959
timeout time.Duration
6060
debug bool
6161

@@ -132,7 +132,6 @@ func (c *Chain) Init(homePath string, timeout time.Duration, codec codec.ProtoCo
132132
c.Client = client
133133
c.HomePath = homePath
134134
c.codec = codec
135-
c.logger = defaultChainLogger()
136135
c.timeout = timeout
137136
c.debug = debug
138137
c.faucetAddrs = make(map[string]time.Time)
@@ -174,6 +173,7 @@ func (c *Chain) RegisterMsgEventListener(listener core.MsgEventListener) {
174173
}
175174

176175
func (c *Chain) sendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, error) {
176+
logger := GetChainLogger()
177177
// broadcast tx
178178
res, _, err := c.rawSendMsgs(msgs)
179179
if err != nil {
@@ -194,7 +194,7 @@ func (c *Chain) sendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, error) {
194194
// call msgEventListener if needed
195195
if c.msgEventListener != nil {
196196
if err := c.msgEventListener.OnSentMsg(msgs); err != nil {
197-
c.logger.Error("failed to OnSendMsg call", "msgs", msgs, "err", err)
197+
logger.Error("failed to OnSendMsg call", err)
198198
return res, nil
199199
}
200200
}
@@ -567,8 +567,9 @@ func newRPCClient(addr string, timeout time.Duration) (*rpchttp.HTTP, error) {
567567
return rpcClient, nil
568568
}
569569

570-
func defaultChainLogger() log.Logger {
571-
return log.NewTMLogger(log.NewSyncWriter(os.Stdout))
570+
func GetChainLogger() *log.RelayLogger {
571+
return log.GetLogger().
572+
WithModule("tendermint.chain")
572573
}
573574

574575
// CreateMnemonic creates a new mnemonic

chains/tendermint/log-chain.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,35 @@ import (
1010

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

2021
if err != nil {
21-
c.logger.Error(fmt.Errorf("- [%s] -> err(%v)", c.ChainID(), err).Error())
22+
logger.Error("failed-tx", err, "chain-id", c.ChainID())
2223
if res == nil {
2324
return
2425
}
2526
}
2627

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

3232
if c.debug && !res.Empty() {
33-
c.Log("- transaction response:")
33+
logger.Info("tx-response", "chain-id", c.ChainID(), "res", res)
3434
c.Print(res, false, false)
3535
}
3636
}
3737

3838
// LogSuccessTx take the transaction and the messages to create it and logs the appropriate data
3939
func (c *Chain) LogSuccessTx(res *sdk.TxResponse, msgs []sdk.Msg) {
40-
c.logger.Info(fmt.Sprintf("✔ [%s]@{%d} - msg(%s) hash(%s)", c.ChainID(), res.Height, getMsgAction(msgs), res.TxHash))
41-
}
42-
43-
// Log takes a string and logs the data
44-
func (c *Chain) Log(s string) {
45-
c.logger.Info(s)
40+
logger := GetChainLogger()
41+
logger.Info("success-tx", "chain-id", c.ChainID(), "height", res.Height, "hash", res.TxHash)
4642
}
4743

4844
// Print fmt.Printlns the json or yaml representation of whatever is passed in

0 commit comments

Comments
 (0)