Skip to content

Commit

Permalink
config: forward fx output to the logger
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Nov 10, 2022
1 parent d0aece4 commit 2d8484b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 2 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ import (
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"

logging "github.com/ipfs/go-log/v2"
ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
)

var log = logging.Logger("p2p-config")

// AddrsFactory is a function that takes a set of multiaddrs we're listening on and
// returns the set of multiaddrs we should advertise to the network.
type AddrsFactory = bhost.AddrsFactory
Expand Down Expand Up @@ -187,7 +185,7 @@ func (cfg *Config) addTransports(h host.Host) error {
}

fxopts := []fx.Option{
fx.NopLogger,
fx.WithLogger(func() fxevent.Logger { return getFXLogger() }),
fx.Provide(tptu.New),
fx.Provide(func() network.Multiplexer { return muxer }),
fx.Provide(fx.Annotate(
Expand Down
28 changes: 28 additions & 0 deletions config/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package config

import (
"strings"
"sync"

logging "github.com/ipfs/go-log/v2"
"go.uber.org/fx/fxevent"
)

var log = logging.Logger("p2p-config")

var (
fxLogger fxevent.Logger
logInitOnce sync.Once
)

type fxLogWriter struct{}

func (l *fxLogWriter) Write(b []byte) (int, error) {
log.Debug(strings.TrimSuffix(string(b), "\n"))
return len(b), nil
}

func getFXLogger() fxevent.Logger {
logInitOnce.Do(func() { fxLogger = &fxevent.ConsoleLogger{W: &fxLogWriter{}} })
return fxLogger
}

0 comments on commit 2d8484b

Please sign in to comment.