Skip to content

Commit

Permalink
Merge pull request #905 from yyforyongyu/fix-logging
Browse files Browse the repository at this point in the history
wallet: downgrade `spew` log to debug and limit its size
  • Loading branch information
Roasbeef authored Jan 27, 2024
2 parents 6b096b0 + 100327c commit 69cbf12
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
17 changes: 17 additions & 0 deletions wallet/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,20 @@ func pickNoun(n int, singular, plural string) string {
}
return plural
}

// LogClosure is a closure that can be printed with %v to be used to
// generate expensive-to-create data for a detailed log level and avoid doing
// the work if the data isn't printed.
type logClosure func() string

// String invokes the log closure and returns the results string.
func (c logClosure) String() string {
return c()
}

// newLogClosure returns a new closure over the passed function which allows
// it to be used as a parameter in a logging function that is only invoked when
// the logging level is such that the message will actually be logged.
func newLogClosure(c func() string) logClosure {
return logClosure(c)
}
20 changes: 14 additions & 6 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3819,15 +3819,23 @@ func (w *Wallet) publishTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) {
log.Warnf("Unable to remove invalid transaction %v: %v",
tx.TxHash(), dbErr)
} else {
// The spew output is pretty nice to directly see how many
// inputs/outputs of what type there are.
log.Infof("Removed invalid transaction: %v", spew.Sdump(tx))
log.Infof("Removed invalid transaction: %v", tx.TxHash())

// The serialized transaction is for logging only, don't fail on
// the error.
// The serialized transaction is for logging only, don't fail
// on the error.
var txRaw bytes.Buffer
_ = tx.Serialize(&txRaw)
log.Infof("Removed invalid transaction: %x", txRaw.Bytes())

// Optionally log the tx in debug when the size is manageable.
if txRaw.Len() < 1_000_000 {
log.Debugf("Removed invalid transaction: %v \n hex=%x",
newLogClosure(func() string {
return spew.Sdump(tx)
}), txRaw.Bytes())
} else {
log.Debug("Removed invalid transaction due to size " +
"too large")
}
}

return nil, returnErr
Expand Down

0 comments on commit 69cbf12

Please sign in to comment.