From dec3e41354a09450fd71dcacbef827666b2aa55e Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 27 Oct 2025 16:03:17 -0700 Subject: [PATCH] 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. --- go.mod | 3 ++- go.sum | 4 ++++ log.go | 21 ++++++++++++++++----- mempool/log.go | 2 +- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 2d543d82..1908db86 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 58fc7324..0a474856 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/log.go b/log.go index 55be25ca..86ee163e 100644 --- a/log.go +++ b/log.go @@ -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 diff --git a/mempool/log.go b/mempool/log.go index 1381af33..0a6343c6 100644 --- a/mempool/log.go +++ b/mempool/log.go @@ -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