config: forward fx output to the logger

This commit is contained in:
Marten Seemann 2022-11-10 10:04:40 +00:00
parent 7d0b6ba933
commit d0704fdc7e
2 changed files with 30 additions and 4 deletions

View File

@ -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
@ -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(

28
config/log.go Normal file
View File

@ -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
}