Skip to content

Commit 6517104

Browse files
committed
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.
1 parent 06f1ef4 commit 6517104

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lnutils/log.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package lnutils
22

33
import (
4+
"log/slog"
45
"strings"
56

7+
"github.com/btcsuite/btcd/btcec/v2"
8+
"github.com/btcsuite/btclog/v2"
69
"github.com/davecgh/go-spew/spew"
710
)
811

@@ -36,3 +39,14 @@ func NewSeparatorClosure() LogClosure {
3639
return strings.Repeat("=", 80)
3740
}
3841
}
42+
43+
// LogPubKey returns a slog attribute for logging a public key in hex format.
44+
func LogPubKey(key string, pubKey *btcec.PublicKey) slog.Attr {
45+
// Handle nil pubkey gracefully, although callers should ideally prevent
46+
// this.
47+
if pubKey == nil {
48+
return btclog.Fmt(key, "<nil>")
49+
}
50+
51+
return btclog.Hex6(key, pubKey.SerializeCompressed())
52+
}

0 commit comments

Comments
 (0)