lnutil: add LogPubKey helper function

This captures a common pattern where we want to log a peer's public key
along side each logging statement.
This commit is contained in:
Olaoluwa Osuntokun
2025-04-16 14:47:18 -07:00
parent 06f1ef47fa
commit 6517104da6

View File

@@ -1,8 +1,11 @@
package lnutils
import (
"log/slog"
"strings"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btclog/v2"
"github.com/davecgh/go-spew/spew"
)
@@ -36,3 +39,14 @@ func NewSeparatorClosure() LogClosure {
return strings.Repeat("=", 80)
}
}
// LogPubKey returns a slog attribute for logging a public key in hex format.
func LogPubKey(key string, pubKey *btcec.PublicKey) slog.Attr {
// Handle nil pubkey gracefully, although callers should ideally prevent
// this.
if pubKey == nil {
return btclog.Fmt(key, "<nil>")
}
return btclog.Hex6(key, pubKey.SerializeCompressed())
}