From e3da04078777e6a1b72ce19981fbd6539b0847d8 Mon Sep 17 00:00:00 2001 From: Awais Rehman <81323680+awaisdev5765@users.noreply.github.com> Date: Tue, 3 Nov 2026 15:57:38 +0500 Subject: [PATCH] Fix metadata priority in MultiplexLogHandler.metadata (#530) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #498 ## Problem `MultiplexLogHandler` had inconsistent metadata priority: - `subscript(metadataKey:)` reads the value from the **first** handler that has the key (documented: "the first handler's values are more important") - `metadata` (full dictionary) merged handlers **last-wins**, so the value came from the **last** handler with that key ## Fix The `metadata` getter now merges handlers in their initialization order, keeping the already-stored value on conflicts — the **first handler wins**, matching the subscript and the docs. The multiplex handler's own `metadataProvider` still takes priority over all underlying handlers/providers, matching its documented role ("runs after all metadata providers of the multiplexed handlers"). ## Testing - Updated `multiplexLogHandlerMetadata_readingHandlerMetadata` and `multiplexMetadataProviderSet`, which encoded the last-wins behavior - Extended `multiplexMetadataProviderSet` with subscript-vs-dictionary consistency checks (both must give the first handler's value on conflicting keys, including provider-provided metadata) - Full suite: 174 tests in 17 suites, all passing --------- Co-authored-by: awaisdev Co-authored-by: awaisdev5765 Co-authored-by: Vladimir Kukushkin --- .../Handlers/MultiplexLogHandler.swift | 13 ++++++------ Tests/LoggingTests/LoggingTest.swift | 21 ++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/Sources/Logging/Handlers/MultiplexLogHandler.swift b/Sources/Logging/Handlers/MultiplexLogHandler.swift index 81779f1..3dceab8 100644 --- a/Sources/Logging/Handlers/MultiplexLogHandler.swift +++ b/Sources/Logging/Handlers/MultiplexLogHandler.swift @@ -190,6 +190,10 @@ public struct MultiplexLogHandler: LogHandler { } /// Get or set the entire metadata storage as a dictionary. + /// + /// The getter merges the metadata of the underlying handlers in initialization order. + /// On a key conflict, the first handler's value wins, as in ``subscript(metadataKey:)``. + /// The dictionary does not contain values from metadata providers. public var metadata: Logger.Metadata { get { var effective: Logger.Metadata = [:] @@ -199,14 +203,9 @@ public struct MultiplexLogHandler: LogHandler { // !-safe, we always have at least one handler effective.reserveCapacity(self.handlers.first!.metadata.count) + // Keep the existing value on a conflict, so the first handler wins. for handler in self.handlers { - effective.merge(handler.metadata, uniquingKeysWith: { _, handlerMetadata in handlerMetadata }) - if let provider = handler.metadataProvider { - effective.merge(provider.get(), uniquingKeysWith: { _, provided in provided }) - } - } - if let provider = self._metadataProvider { - effective.merge(provider.get(), uniquingKeysWith: { _, provided in provided }) + effective.merge(handler.metadata, uniquingKeysWith: { current, _ in current }) } return effective diff --git a/Tests/LoggingTests/LoggingTest.swift b/Tests/LoggingTests/LoggingTest.swift index c292d3d..6f6a90e 100644 --- a/Tests/LoggingTests/LoggingTest.swift +++ b/Tests/LoggingTests/LoggingTest.swift @@ -278,7 +278,8 @@ struct LoggingTest { multiplexLogger.handler.metadata == [ "one": "111", "two": "222", - "in": "in-2", + // First handler wins on conflicting keys (see issue #498): + "in": "in-1", ] ) } @@ -315,21 +316,17 @@ struct LoggingTest { } ) + // The subscript and `metadata` agree on conflicting keys. + #expect(multiplexLogger[metadataKey: "one"] == "111") + #expect(multiplexLogger[metadataKey: "two"] == "222") + #expect(multiplexLogger[metadataKey: "in"] == "in-1") + + // Provider values are not part of `metadata`. #expect( multiplexLogger.handler.metadata == [ "one": "111", "two": "222", - "in": "in-2", - "provider-1": "provided-111", - "provider-2": "provided-222", - "provider-overlap": "provided-222", - ] - ) - #expect( - multiplexLogger.handler.metadataProvider?.get() == [ - "provider-1": "provided-111", - "provider-2": "provided-222", - "provider-overlap": "provided-222", + "in": "in-1", ] ) }