deps+log: upgrade to btclog/v2 with dual logger support

This commit upgrades the btclog dependency from v0 to support both v1 and
v2 loggers simultaneously. The dual logger pattern, inspired by LND's
implementation, allows us to incrementally adopt btclog/v2's structured
logging capabilities in the mempool subsystem while maintaining backward
compatibility with the rest of the codebase.

The key changes enable structured logging (DebugS, InfoS, WarnS, etc.) with
key-value pair attributes for rich operational context, particularly valuable
for security monitoring and debugging in the mempool implementation.

We create separate backends: backendLog using btclogv1 for existing
subsystems, and backendLogV2 using btclog v2 for the mempool. The
setLogLevel function now handles both logger types via type switching,
ensuring log level changes work across both versions.
This commit is contained in:
Olaoluwa Osuntokun 2025-10-27 16:03:17 -07:00
parent 78a45dedea
commit dec3e41354
4 changed files with 23 additions and 7 deletions

3
go.mod
View file

@ -5,7 +5,7 @@ require (
github.com/btcsuite/btcd/btcutil v1.1.5
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
github.com/btcsuite/btcd/v2transport v1.0.1
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792
github.com/btcsuite/winsvc v1.0.0
@ -24,6 +24,7 @@ require (
require (
github.com/aead/siphash v1.0.1 // indirect
github.com/btcsuite/btclog/v2 v2.0.0 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23 // indirect

4
go.sum
View file

@ -19,6 +19,10 @@ github.com/btcsuite/btcd/v2transport v1.0.1 h1:pIyyyBCPwd087K3Wdb/9tIvUubAQdzTJg
github.com/btcsuite/btcd/v2transport v1.0.1/go.mod h1:N6H0HGSElVVJKntzaYHYVbW71DtWDLMw2yhwVRO3ZOE=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9VhRV3jjAVU7DJVjMaK+IsvSeZvFo=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c h1:4HxD1lBUGUddhzgaNgrCPsFWd7cGYNpeFUgd9ZIgyM0=
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c/go.mod h1:w7xnGOhwT3lmrS4H3b/D1XAXxvh+tbhUm8xeHN2y3TQ=
github.com/btcsuite/btclog/v2 v2.0.0 h1:ZfOBItEeLWfU0voi88K72j8vtxP4/dHhxRFf2bxZkVo=
github.com/btcsuite/btclog/v2 v2.0.0/go.mod h1:XItGUfVOxotJL8kkuk2Hj3EVow5KCugXl3wWfQ6K0AE=
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd h1:R/opQEbFEy9JGkIguV40SvRY1uliPX8ifOvi6ICsFCw=
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=

21
log.go
View file

@ -23,7 +23,8 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/v2transport"
"github.com/btcsuite/btclog"
btclogv1 "github.com/btcsuite/btclog"
"github.com/btcsuite/btclog/v2"
"github.com/jrick/logrotate/rotator"
)
@ -49,7 +50,10 @@ var (
// backendLog is the logging backend used to create all subsystem loggers.
// The backend must not be used before the log rotator has been initialized,
// or data races and/or nil pointer dereferences will occur.
backendLog = btclog.NewBackend(logWriter{})
backendLog = btclogv1.NewBackend(logWriter{})
// backendLogV2 is the v2 logging backend for subsystems using btclog/v2.
backendLogV2 = btclog.NewSLogger(btclog.NewDefaultHandler(logWriter{}))
// logRotator is one of the logging outputs. It should be closed on
// application shutdown.
@ -69,7 +73,7 @@ var (
scrpLog = backendLog.Logger("SCRP")
srvrLog = backendLog.Logger("SRVR")
syncLog = backendLog.Logger("SYNC")
txmpLog = backendLog.Logger("TXMP")
txmpLog = backendLogV2.SubSystem("TXMP")
v2trLog = backendLog.Logger(v2transport.Subsystem)
)
@ -90,7 +94,7 @@ func init() {
}
// subsystemLoggers maps each subsystem identifier to its associated logger.
var subsystemLoggers = map[string]btclog.Logger{
var subsystemLoggers = map[string]any{
"ADXR": adxrLog,
"AMGR": amgrLog,
"CMGR": cmgrLog,
@ -140,7 +144,14 @@ func setLogLevel(subsystemID string, logLevel string) {
// Defaults to info if the log level is invalid.
level, _ := btclog.LevelFromString(logLevel)
logger.SetLevel(level)
// Handle both v1 and v2 loggers.
switch l := logger.(type) {
case btclogv1.Logger:
l.SetLevel(btclogv1.Level(level))
case btclog.Logger:
l.SetLevel(level)
}
}
// setLogLevels sets the log level for all subsystem loggers to the passed

View file

@ -5,7 +5,7 @@
package mempool
import (
"github.com/btcsuite/btclog"
"github.com/btcsuite/btclog/v2"
)
// log is a logger that is initialized with no output filters. This