From c48cf7783e7ef37e4185a06d06690a822b17eccd Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 16 Nov 2026 08:59:44 +0100 Subject: [PATCH] wip Signed-off-by: Jakob Borg --- internal/dht/logging.go | 2 ++ internal/dht/logging_test.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/internal/dht/logging.go b/internal/dht/logging.go index 557b4b0b5..254f554cb 100644 --- a/internal/dht/logging.go +++ b/internal/dht/logging.go @@ -12,6 +12,7 @@ import ( "sync" logging "github.com/ipfs/go-log/v2" + "github.com/libp2p/go-libp2p/gologshim" "github.com/syncthing/syncthing/internal/slogutil" "go.uber.org/zap/zapcore" ) @@ -26,6 +27,7 @@ func setupLibraryLogging() { // the normal Syncthing log stream and support bundles. The slog handler // still controls whether debug records are emitted. logging.SetPrimaryCore(libraryLogCore{}) + gologshim.SetDefaultHandler(logging.SlogHandler()) for _, system := range []string{"dht", "dht/RtRefreshManager", "swarm2", "autonat"} { _ = logging.SetLogLevel(system, "debug") } diff --git a/internal/dht/logging_test.go b/internal/dht/logging_test.go index 3bd9e5afe..1b32fb5cb 100644 --- a/internal/dht/logging_test.go +++ b/internal/dht/logging_test.go @@ -13,6 +13,7 @@ import ( "strings" "testing" + "github.com/libp2p/go-libp2p/gologshim" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -45,3 +46,19 @@ func TestLibraryLogCore(t *testing.T) { t.Fatal("verbose library info should require debug logging") } } + +// libp2p's native slog loggers need an explicit bridge in addition to the +// older DHT dependency's Zap loggers. +func TestLibraryLogShim(t *testing.T) { + var output bytes.Buffer + previous := slog.Default() + slog.SetDefault(slog.New(slog.NewTextHandler(&output, &slog.HandlerOptions{Level: slog.LevelDebug}))) + t.Cleanup(func() { slog.SetDefault(previous) }) + setupLibraryLogging() + gologshim.Logger("autonat").Debug("test dialback result", "peer", "test-peer", "err", errors.New("dial failed")) + for _, want := range []string{"test dialback result", "test-peer", "dial failed", "autonat"} { + if !strings.Contains(output.String(), want) { + t.Errorf("missing %q in %s", want, output.String()) + } + } +}