From 6127b4def05ae1a6fa32e4b21c36a3d7b6bfee7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Lewandowski?= <35259896+pawellewandowski98@users.noreply.github.com> Date: Fri, 6 Dec 2024 12:15:38 +0100 Subject: [PATCH] feat(ARCO-295): new log level in p2p (#695) --- cmd/arc/services/blocktx.go | 4 ++++ cmd/arc/services/metamorph.go | 4 ++++ config/config.go | 1 + config/defaults.go | 1 + config/example_config.yaml | 3 ++- internal/p2p/peer.go | 16 ++++++++-------- internal/p2p/peer_options.go | 11 +++++++++++ test/config/config.yaml | 3 ++- 8 files changed, 33 insertions(+), 10 deletions(-) diff --git a/cmd/arc/services/blocktx.go b/cmd/arc/services/blocktx.go index ba1e0771f..6e8140070 100644 --- a/cmd/arc/services/blocktx.go +++ b/cmd/arc/services/blocktx.go @@ -165,6 +165,10 @@ func StartBlockTx(logger *slog.Logger, arcConfig *config.ArcConfig) (func(), err peerOpts = append(peerOpts, p2p.WithUserAgent("ARC", version.Version)) } + if arcConfig.LogLevel != arcConfig.PeerLogLevel { + peerOpts = append(peerOpts, p2p.WithLogLevel(arcConfig.PeerLogLevel, arcConfig.LogFormat)) + } + for i, peerSetting := range arcConfig.Broadcasting.Unicast.Peers { peerURL, err := peerSetting.GetP2PUrl() if err != nil { diff --git a/cmd/arc/services/metamorph.go b/cmd/arc/services/metamorph.go index e850ff808..7c841307a 100644 --- a/cmd/arc/services/metamorph.go +++ b/cmd/arc/services/metamorph.go @@ -297,6 +297,10 @@ func initPeerManager(logger *slog.Logger, s store.MetamorphStore, arcConfig *con peerOpts = append(peerOpts, p2p.WithUserAgent("ARC", version.Version)) } + if arcConfig.LogLevel != arcConfig.PeerLogLevel { + peerOpts = append(peerOpts, p2p.WithLogLevel(arcConfig.PeerLogLevel, arcConfig.LogFormat)) + } + l := logger.With(slog.String("module", "peer")) for _, peerSetting := range arcConfig.Broadcasting.Unicast.Peers { peerURL, err := peerSetting.GetP2PUrl() diff --git a/config/config.go b/config/config.go index a85c7f03f..f3f158c75 100644 --- a/config/config.go +++ b/config/config.go @@ -14,6 +14,7 @@ const ( type ArcConfig struct { LogLevel string `mapstructure:"logLevel"` + PeerLogLevel string `mapstructure:"peerLogLevel"` LogFormat string `mapstructure:"logFormat"` ProfilerAddr string `mapstructure:"profilerAddr"` Prometheus *PrometheusConfig `mapstructure:"prometheus"` diff --git a/config/defaults.go b/config/defaults.go index ffc6b8336..530d1cf71 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -9,6 +9,7 @@ import ( func getDefaultArcConfig() *ArcConfig { return &ArcConfig{ LogLevel: "DEBUG", + PeerLogLevel: "DEBUG", LogFormat: "text", ProfilerAddr: "", // optional Prometheus: getDefaultPrometheusConfig(), diff --git a/config/example_config.yaml b/config/example_config.yaml index b3709e38d..a3f1cfd48 100644 --- a/config/example_config.yaml +++ b/config/example_config.yaml @@ -1,6 +1,7 @@ --- -logLevel: DEBUG # mode of logging. Value can be one of TRACE | DEBUG | INFO | WARN | ERROR logFormat: text # format of logging. Value can be one of text | json | tint +logLevel: DEBUG # mode of logging. Value can be one of DEBUG | INFO | WARN | ERROR +peerLogLevel: DEBUG # mode of logging for peer. Value can be one of TRACE | DEBUG | INFO | WARN | ERROR profilerAddr: localhost:9999 # address to start profiler server on (optional) prometheus: enabled: false # if true, then prometheus metrics are enabled diff --git a/internal/p2p/peer.go b/internal/p2p/peer.go index 1443883a1..33c3f35c8 100644 --- a/internal/p2p/peer.go +++ b/internal/p2p/peer.go @@ -59,16 +59,9 @@ type Peer struct { } func NewPeer(logger *slog.Logger, msgHandler MessageHandlerI, address string, network wire.BitcoinNet, options ...PeerOptions) *Peer { - l := logger.With( - slog.Group("peer", - slog.String("network", network.String()), - slog.String("address", address), - ), - ) - p := &Peer{ dial: net.Dial, - l: l, + l: logger, mh: msgHandler, address: address, @@ -88,6 +81,13 @@ func NewPeer(logger *slog.Logger, msgHandler MessageHandlerI, address string, ne opt(p) } + p.l = p.l.With( + slog.Group("peer", + slog.String("network", network.String()), + slog.String("address", address), + ), + ) + if p.writeCh == nil { p.writeCh = make(chan wire.Message, 128) } diff --git a/internal/p2p/peer_options.go b/internal/p2p/peer_options.go index 9306bd272..2eb5c0872 100644 --- a/internal/p2p/peer_options.go +++ b/internal/p2p/peer_options.go @@ -4,6 +4,7 @@ import ( "net" "time" + "github.com/bitcoin-sv/arc/internal/logger" "github.com/libsv/go-p2p/wire" ) @@ -46,3 +47,13 @@ func WithDialer(dial func(network, address string) (net.Conn, error)) PeerOption p.dial = dial } } + +func WithLogLevel(level, logFormat string) PeerOptions { + return func(p *Peer) { + l, err := logger.NewLogger(level, logFormat) + if err != nil { + l, _ = logger.NewLogger("DEBUG", "json") + } + p.l = l + } +} diff --git a/test/config/config.yaml b/test/config/config.yaml index 937ff7dad..d52fb06d2 100644 --- a/test/config/config.yaml +++ b/test/config/config.yaml @@ -1,6 +1,7 @@ --- -logLevel: INFO logFormat: tint +logLevel: INFO +peerLogLevel: DEBUG profilerAddr: localhost:9999 prometheus: enabled: true