diff --git a/wallet/log.go b/wallet/log.go index de39fe1831..7e8a72a193 100644 --- a/wallet/log.go +++ b/wallet/log.go @@ -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) +} diff --git a/wallet/wallet.go b/wallet/wallet.go index 17d55b826a..335025dab4 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -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