Skip to content

Commit

Permalink
feat(ARCO-295): new log level in p2p (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 authored and arkadiuszos4chain committed Dec 10, 2024
1 parent 51a3683 commit 6127b4d
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 10 deletions.
4 changes: 4 additions & 0 deletions cmd/arc/services/blocktx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions cmd/arc/services/metamorph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
func getDefaultArcConfig() *ArcConfig {
return &ArcConfig{
LogLevel: "DEBUG",
PeerLogLevel: "DEBUG",
LogFormat: "text",
ProfilerAddr: "", // optional
Prometheus: getDefaultPrometheusConfig(),
Expand Down
3 changes: 2 additions & 1 deletion config/example_config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 8 additions & 8 deletions internal/p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
11 changes: 11 additions & 0 deletions internal/p2p/peer_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"
"time"

"github.com/bitcoin-sv/arc/internal/logger"
"github.com/libsv/go-p2p/wire"
)

Expand Down Expand Up @@ -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
}
}
3 changes: 2 additions & 1 deletion test/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
logLevel: INFO
logFormat: tint
logLevel: INFO
peerLogLevel: DEBUG
profilerAddr: localhost:9999
prometheus:
enabled: true
Expand Down

0 comments on commit 6127b4d

Please sign in to comment.