From c7413d967b97aae67c46452bf7645bdb7b200e8e Mon Sep 17 00:00:00 2001 From: 0z5a Date: Tue, 17 Nov 2026 14:24:42 +0100 Subject: [PATCH] Make `MultiplexLogHandler.metadataProvider` setter replace the initializer's provider (#537) ### Motivation: `MultiplexLogHandler.metadataProvider`'s setter only forwards the new provider to the underlying handlers. Since #522, `log(event:)` also merges the provider passed to `init(_:metadataProvider:)` on top of the underlying handlers' metadata, so after a set that old provider still overrides the new one for conflicting keys, setting `nil` doesn't remove it, and the getter keeps returning it. This shows up through `Logger(label:metadataProvider:)`, which calls the factory and then sets `handler.metadataProvider`. If the bootstrapped factory returns a `MultiplexLogHandler` with its own provider, the provider the library passed is silently overridden. With a plain handler the same code uses the library's provider, which is what `Logger(label:metadataProvider:)` documents ("the custom metadata provider this logger should invoke, instead of the system wide bootstrapped one"). ### Modifications: - In the setter, also replace the multiplex handler's own provider with the new value. It is still forwarded to the underlying handlers as before. - Document the setter. - Add regression tests: setting a provider replaces the initializer's provider for one and two handlers, and setting `nil` clears it (both the getter and emitted logs). Another test covers an underlying handler that doesn't support metadata providers. I kept forwarding to the underlying handlers and replace the multiplex handler's own provider as well, rather than clearing it. Clearing it would drop provider metadata for underlying handlers that use the default `metadataProvider` implementation in the `Logger(label:metadataProvider:)` case, which works today. ### Result: After `handler.metadataProvider = p`, logs and the getter use `p`. After `handler.metadataProvider = nil`, no provider metadata is added. In the common setup where the factory passes its provider into the `MultiplexLogHandler` initializer, the number of provider calls per log stays the same. When the multiplex handler had no provider of its own, a set now also applies the provider once at the multiplex level, so underlying handlers without provider support get its metadata too. The new tests fail on main and pass with this change. All tests pass locally on macOS (Swift 6.3.3), and `swift format lint` is clean on the changed files. I couldn't run the benchmarks locally. This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described. --- .../Handlers/MultiplexLogHandler.swift | 6 +++ .../MultiplexProviderRegressionTests.swift | 48 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/Sources/Logging/Handlers/MultiplexLogHandler.swift b/Sources/Logging/Handlers/MultiplexLogHandler.swift index 3dceab8..b25f86a 100644 --- a/Sources/Logging/Handlers/MultiplexLogHandler.swift +++ b/Sources/Logging/Handlers/MultiplexLogHandler.swift @@ -105,6 +105,9 @@ public struct MultiplexLogHandler: LogHandler { } /// The metadata provider. + /// + /// Setting this property sets the provider on all underlying handlers and replaces + /// the provider passed to ``init(_:metadataProvider:)``. public var metadataProvider: Logger.MetadataProvider? { get { if self.handlers.count == 1 { @@ -138,6 +141,9 @@ public struct MultiplexLogHandler: LogHandler { } } set { + // Also replace this handler's own provider, otherwise the provider passed to the + // initializer keeps overriding the new one, and setting `nil` would not remove it. + self._metadataProvider = newValue self.mutatingForEachHandler { $0.metadataProvider = newValue } } } diff --git a/Tests/InMemoryLoggingTests/MultiplexProviderRegressionTests.swift b/Tests/InMemoryLoggingTests/MultiplexProviderRegressionTests.swift index 1d3b8ef..8ed6ab8 100644 --- a/Tests/InMemoryLoggingTests/MultiplexProviderRegressionTests.swift +++ b/Tests/InMemoryLoggingTests/MultiplexProviderRegressionTests.swift @@ -45,4 +45,52 @@ struct MultiplexProviderRegressionTests { #expect(sink.entries.first?.metadata == ["child": "kept", "outer": "kept", "shared": "outer"]) #expect(sink.entries.last?.metadata == ["child": "kept", "outer": "kept", "shared": "explicit"]) } + + @Test(arguments: [1, 2]) + func settingProviderReplacesConstructorProvider(handlerCount: Int) { + let sinks = (0.. Logger.Metadata.Value? { + get { self.metadata[key] } + set { self.metadata[key] = newValue } + } + + func log(event: LogEvent) { + self.sink.log(event: event) + } }