From b98fc168ecaf3d0d06806ce435fbddaee748fb34 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 25 Nov 2024 12:05:44 +0200 Subject: [PATCH 1/2] go.mod+build: update btclog dep --- build/config.go | 9 ++++----- build/handler_sets.go | 13 +++++++++++++ build/prefix_log.go | 13 +++++++++++++ go.mod | 2 +- go.sum | 4 ++-- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/build/config.go b/build/config.go index fd3b01a94ad..87b9ab9aec3 100644 --- a/build/config.go +++ b/build/config.go @@ -67,11 +67,10 @@ func DefaultLogConfig() *LogConfig { // config struct translates to. func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption { opts := []btclog.HandlerOption{ - // The default skip depth used by the logging library is 6 but - // since we wrap the logging handlers with another level of - // abstraction with the handlerSet, we increase the skip depth - // to 7 here. - btclog.WithCallSiteSkipDepth(7), + // We wrap the logger provided by the logging library with + // another layer of abstraction with the handlerSet, and so we + // need to increase the default skip depth by 1. + btclog.WithCallSiteSkipDepth(btclog.DefaultSkipDepth + 1), } if cfg.NoTimestamps { diff --git a/build/handler_sets.go b/build/handler_sets.go index d9786a85801..485aca2d6ad 100644 --- a/build/handler_sets.go +++ b/build/handler_sets.go @@ -108,6 +108,19 @@ func (h *handlerSet) Level() btclogv1.Level { return h.level } +// WithPrefix returns a copy of the Handler but with the given string prefixed +// to each log message. +// +// NOTE: this is part of the btclog.Handler interface. +func (h *handlerSet) WithPrefix(prefix string) btclog.Handler { + newSet := &handlerSet{set: make([]btclog.Handler, len(h.set))} + for i, handler := range h.set { + newSet.set[i] = handler.WithPrefix(prefix) + } + + return newSet +} + // A compile-time check to ensure that handlerSet implements btclog.Handler. var _ btclog.Handler = (*handlerSet)(nil) diff --git a/build/prefix_log.go b/build/prefix_log.go index 3db114ffcf4..22f4318ef34 100644 --- a/build/prefix_log.go +++ b/build/prefix_log.go @@ -163,5 +163,18 @@ func (p *PrefixLog) SetLevel(level btclogv1.Level) { p.log.SetLevel(level) } +// SubSystem returns a copy of the logger but with the new subsystem tag. Any +// previously set prefix will be overridden with an empty string. +func (p *PrefixLog) SubSystem(tag string) btclog.Logger { + return p.log.SubSystem(tag) +} + +// WithPrefix returns a copy of the logger but with the given string prefixed to +// each log message. Note that the subsystem of the original logger is kept but +// any existing prefix is overridden. +func (p *PrefixLog) WithPrefix(prefix string) btclog.Logger { + return p.log.WithPrefix(prefix) +} + // Assert that PrefixLog fulfills the btclog.Logger interface. var _ btclog.Logger = &PrefixLog{} diff --git a/go.mod b/go.mod index d0ff97ac7cc..6ca886630b9 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/btcsuite/btcd/btcutil/psbt v1.1.8 github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c - github.com/btcsuite/btclog/v2 v2.0.0-20241017175713-3428138b75c7 + github.com/btcsuite/btclog/v2 v2.0.0 github.com/btcsuite/btcwallet v0.16.10-0.20241113134707-b4ff60753aaa github.com/btcsuite/btcwallet/wallet/txauthor v1.3.5 github.com/btcsuite/btcwallet/wallet/txrules v1.2.2 diff --git a/go.sum b/go.sum index 2ea42fd8ce0..aa04dc5fce8 100644 --- a/go.sum +++ b/go.sum @@ -92,8 +92,8 @@ github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtyd 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-20241017175713-3428138b75c7 h1:3Ct3zN3VCEKVm5nceWBBEKczc+jvTfVyOEG71ob2Yuc= -github.com/btcsuite/btclog/v2 v2.0.0-20241017175713-3428138b75c7/go.mod h1:XItGUfVOxotJL8kkuk2Hj3EVow5KCugXl3wWfQ6K0AE= +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/btcwallet v0.16.10-0.20241113134707-b4ff60753aaa h1:x7vYpwkPL5zeJEWPPaRunybH9ERRMGWeNf7x/0aU/38= github.com/btcsuite/btcwallet v0.16.10-0.20241113134707-b4ff60753aaa/go.mod h1:1HJXYbjJzgumlnxOC2+ViR1U+gnHWoOn7WeK5OfY1eU= From c2923e2214ffc9413cda0097d3bb6f07d01ba98e Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 25 Nov 2024 12:11:32 +0200 Subject: [PATCH 2/2] multi: remove PrefixLog And instead use the new btclog Logger `WithPrefix` method. --- build/prefix_log.go | 180 ----------------------------- contractcourt/contract_resolver.go | 3 +- htlcswitch/link.go | 3 +- htlcswitch/quiescer.go | 3 +- lnwallet/channel.go | 3 +- peer/brontide.go | 3 +- routing/missioncontrol.go | 13 +-- routing/payment_session.go | 3 +- watchtower/wtclient/client.go | 5 +- 9 files changed, 13 insertions(+), 203 deletions(-) delete mode 100644 build/prefix_log.go diff --git a/build/prefix_log.go b/build/prefix_log.go deleted file mode 100644 index 22f4318ef34..00000000000 --- a/build/prefix_log.go +++ /dev/null @@ -1,180 +0,0 @@ -package build - -import ( - "context" - - btclogv1 "github.com/btcsuite/btclog" - "github.com/btcsuite/btclog/v2" -) - -// PrefixLog is a pass-through logger that adds a prefix to every logged line. -type PrefixLog struct { - log btclog.Logger - prefix string - attr []any -} - -// NewPrefixLog instantiates a new prefixed logger. -func NewPrefixLog(prefix string, log btclog.Logger, attrs ...any) *PrefixLog { - return &PrefixLog{ - log: log, - prefix: prefix, - attr: attrs, - } -} - -// addFormatPrefix prepends the prefix to a format string. -func (p *PrefixLog) addFormatPrefix(s string) string { - return p.prefix + " " + s -} - -// addArgsPrefix prepends the prefix to a list of arguments. -func (p *PrefixLog) addArgsPrefix(args []any) []any { - return append([]interface{}{p.prefix}, args...) -} - -// mergeAttr merges the given set of attributes with any attributes that the -// logger was initialised with. -func (p *PrefixLog) mergeAttr(attrs []any) []any { - return append(append([]any{}, attrs...), p.attr...) -} - -// Tracef formats message according to format specifier and writes to to log -// with LevelTrace. -func (p *PrefixLog) Tracef(format string, params ...interface{}) { - p.log.Tracef(p.addFormatPrefix(format), params...) -} - -// Debugf formats message according to format specifier and writes to log with -// LevelDebug. -func (p *PrefixLog) Debugf(format string, params ...interface{}) { - p.log.Debugf(p.addFormatPrefix(format), params...) -} - -// Infof formats message according to format specifier and writes to log with -// LevelInfo. -func (p *PrefixLog) Infof(format string, params ...interface{}) { - p.log.Infof(p.addFormatPrefix(format), params...) -} - -// Warnf formats message according to format specifier and writes to to log with -// LevelWarn. -func (p *PrefixLog) Warnf(format string, params ...interface{}) { - p.log.Warnf(p.addFormatPrefix(format), params...) -} - -// Errorf formats message according to format specifier and writes to to log -// with LevelError. -func (p *PrefixLog) Errorf(format string, params ...interface{}) { - p.log.Errorf(p.addFormatPrefix(format), params...) -} - -// Criticalf formats message according to format specifier and writes to log -// with LevelCritical. -func (p *PrefixLog) Criticalf(format string, params ...interface{}) { - p.log.Criticalf(p.addFormatPrefix(format), params...) -} - -// Trace formats message using the default formats for its operands and writes -// to log with LevelTrace. -func (p *PrefixLog) Trace(v ...interface{}) { - p.log.Trace(p.addArgsPrefix(v)...) -} - -// Debug formats message using the default formats for its operands and writes -// to log with LevelDebug. -func (p *PrefixLog) Debug(v ...interface{}) { - p.log.Debug(p.addArgsPrefix(v)...) -} - -// Info formats message using the default formats for its operands and writes to -// log with LevelInfo. -func (p *PrefixLog) Info(v ...interface{}) { - p.log.Info(p.addArgsPrefix(v)...) -} - -// Warn formats message using the default formats for its operands and writes to -// log with LevelWarn. -func (p *PrefixLog) Warn(v ...interface{}) { - p.log.Warn(p.addArgsPrefix(v)...) -} - -// Error formats message using the default formats for its operands and writes -// to log with LevelError. -func (p *PrefixLog) Error(v ...interface{}) { - p.log.Error(p.addArgsPrefix(v)...) -} - -// Critical formats message using the default formats for its operands and -// writes to log with LevelCritical. -func (p *PrefixLog) Critical(v ...interface{}) { - p.log.Critical(p.addArgsPrefix(v)...) -} - -// TraceS writes a structured log with the given message and key-value pair -// attributes with LevelTrace to the log. -func (p *PrefixLog) TraceS(ctx context.Context, msg string, attrs ...any) { - p.log.TraceS(ctx, p.addFormatPrefix(msg), p.mergeAttr(attrs)...) -} - -// DebugS writes a structured log with the given message and key-value pair -// attributes with LevelDebug to the log. -func (p *PrefixLog) DebugS(ctx context.Context, msg string, attrs ...any) { - p.log.DebugS(ctx, p.addFormatPrefix(msg), p.mergeAttr(attrs)...) -} - -// InfoS writes a structured log with the given message and key-value pair -// attributes with LevelInfo to the log. -func (p *PrefixLog) InfoS(ctx context.Context, msg string, attrs ...any) { - p.log.InfoS(ctx, p.addFormatPrefix(msg), p.mergeAttr(attrs)...) -} - -// WarnS writes a structured log with the given message and key-value pair -// attributes with LevelWarn to the log. -func (p *PrefixLog) WarnS(ctx context.Context, msg string, err error, - attrs ...any) { - - p.log.WarnS(ctx, p.addFormatPrefix(msg), err, p.mergeAttr(attrs)...) -} - -// ErrorS writes a structured log with the given message and key-value pair -// attributes with LevelError to the log. -func (p *PrefixLog) ErrorS(ctx context.Context, msg string, err error, - attrs ...any) { - - p.log.ErrorS(ctx, p.addFormatPrefix(msg), err, p.mergeAttr(attrs)...) -} - -// CriticalS writes a structured log with the given message and key-value pair -// attributes with LevelCritical to the log. -func (p *PrefixLog) CriticalS(ctx context.Context, msg string, err error, - attrs ...any) { - - p.log.CriticalS(ctx, p.addFormatPrefix(msg), err, p.mergeAttr(attrs)...) -} - -// Level returns the current logging level. -func (p *PrefixLog) Level() btclogv1.Level { - return p.log.Level() -} - -// SetLevel changes the logging level to the passed level. -func (p *PrefixLog) SetLevel(level btclogv1.Level) { - p.log.SetLevel(level) -} - -// SubSystem returns a copy of the logger but with the new subsystem tag. Any -// previously set prefix will be overridden with an empty string. -func (p *PrefixLog) SubSystem(tag string) btclog.Logger { - return p.log.SubSystem(tag) -} - -// WithPrefix returns a copy of the logger but with the given string prefixed to -// each log message. Note that the subsystem of the original logger is kept but -// any existing prefix is overridden. -func (p *PrefixLog) WithPrefix(prefix string) btclog.Logger { - return p.log.WithPrefix(prefix) -} - -// Assert that PrefixLog fulfills the btclog.Logger interface. -var _ btclog.Logger = &PrefixLog{} diff --git a/contractcourt/contract_resolver.go b/contractcourt/contract_resolver.go index 691822610a4..53f4f680d00 100644 --- a/contractcourt/contract_resolver.go +++ b/contractcourt/contract_resolver.go @@ -8,7 +8,6 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btclog/v2" - "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/fn" ) @@ -123,7 +122,7 @@ func newContractResolverKit(cfg ResolverConfig) *contractResolverKit { // initLogger initializes the resolver-specific logger. func (r *contractResolverKit) initLogger(resolver ContractResolver) { logPrefix := fmt.Sprintf("%T(%v):", resolver, r.ChanPoint) - r.log = build.NewPrefixLog(logPrefix, log) + r.log = log.WithPrefix(logPrefix) } var ( diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 3eb398c1af1..57904600555 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -14,7 +14,6 @@ import ( "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btclog/v2" - "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb/models" "github.com/lightningnetwork/lnd/contractcourt" @@ -508,7 +507,7 @@ func NewChannelLink(cfg ChannelLinkConfig, channel: channel, hodlMap: make(map[models.CircuitKey]hodlHtlc), hodlQueue: queue.NewConcurrentQueue(10), - log: build.NewPrefixLog(logPrefix, log), + log: log.WithPrefix(logPrefix), flushHooks: newHookMap(), outgoingCommitHooks: newHookMap(), incomingCommitHooks: newHookMap(), diff --git a/htlcswitch/quiescer.go b/htlcswitch/quiescer.go index 5a762215765..27d0deb8c67 100644 --- a/htlcswitch/quiescer.go +++ b/htlcswitch/quiescer.go @@ -6,7 +6,6 @@ import ( "time" "github.com/btcsuite/btclog/v2" - "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/fn" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwire" @@ -178,7 +177,7 @@ func NewQuiescer(cfg QuiescerCfg) Quiescer { return &QuiescerLive{ cfg: cfg, - log: build.NewPrefixLog(logPrefix, log), + log: log.WithPrefix(logPrefix), } } diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 74ecd17454d..fe4351c4764 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -23,7 +23,6 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btclog/v2" "github.com/davecgh/go-spew/spew" - "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb/models" @@ -955,7 +954,7 @@ func NewLightningChannel(signer input.Signer, updateLogs: updateLogs, Capacity: state.Capacity, taprootNonceProducer: taprootNonceProducer, - log: build.NewPrefixLog(logPrefix, walletLog), + log: walletLog.WithPrefix(logPrefix), opts: opts, } diff --git a/peer/brontide.go b/peer/brontide.go index 3bae1be1bc3..bb4c9f96db6 100644 --- a/peer/brontide.go +++ b/peer/brontide.go @@ -20,7 +20,6 @@ import ( "github.com/btcsuite/btclog/v2" "github.com/davecgh/go-spew/spew" "github.com/lightningnetwork/lnd/buffer" - "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb/models" @@ -624,7 +623,7 @@ func NewBrontide(cfg Config) *Brontide { resentChanSyncMsg: make(map[lnwire.ChannelID]struct{}), startReady: make(chan struct{}), quit: make(chan struct{}), - log: build.NewPrefixLog(logPrefix, peerLog), + log: peerLog.WithPrefix(logPrefix), msgRouter: msgRouter, globalMsgRouter: globalMsgRouter, } diff --git a/routing/missioncontrol.go b/routing/missioncontrol.go index 20aa0490737..de892392e75 100644 --- a/routing/missioncontrol.go +++ b/routing/missioncontrol.go @@ -11,7 +11,6 @@ import ( "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btclog/v2" "github.com/btcsuite/btcwallet/walletdb" - "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/fn" @@ -411,13 +410,13 @@ func (m *MissionController) initMissionControl(namespace string) ( } mc := &MissionControl{ - cfg: m.cfg, - state: newMissionControlState(cfg.MinFailureRelaxInterval), - store: store, - estimator: cfg.Estimator, - log: build.NewPrefixLog( - fmt.Sprintf("[%s]:", namespace), log, + cfg: m.cfg, + state: newMissionControlState( + cfg.MinFailureRelaxInterval, ), + store: store, + estimator: cfg.Estimator, + log: log.WithPrefix(fmt.Sprintf("[%s]:", namespace)), onConfigUpdate: cfg.OnConfigUpdate, } diff --git a/routing/payment_session.go b/routing/payment_session.go index 2ab9e4fd41a..3cbacad89c3 100644 --- a/routing/payment_session.go +++ b/routing/payment_session.go @@ -5,7 +5,6 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btclog/v2" - "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb/models" "github.com/lightningnetwork/lnd/lnutils" @@ -232,7 +231,7 @@ func newPaymentSession(p *LightningPayment, selfNode route.Vertex, pathFindingConfig: pathFindingConfig, missionControl: missionControl, minShardAmt: DefaultShardMinAmt, - log: build.NewPrefixLog(logPrefix, log), + log: log.WithPrefix(logPrefix), }, nil } diff --git a/watchtower/wtclient/client.go b/watchtower/wtclient/client.go index 6cf5d9114a7..b3e322faa3b 100644 --- a/watchtower/wtclient/client.go +++ b/watchtower/wtclient/client.go @@ -12,7 +12,6 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btclog/v2" - "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/lnwallet" @@ -210,9 +209,7 @@ func newClient(cfg *clientCfg) (*client, error) { if err != nil { return nil, err } - prefix := fmt.Sprintf("(%s)", identifier) - - plog := build.NewPrefixLog(prefix, log) + plog := log.WithPrefix(fmt.Sprintf("(%s)", identifier)) queueDB := cfg.DB.GetDBQueue([]byte(identifier)) queue, err := NewDiskOverflowQueue[*wtdb.BackupID](