Fix metadata priority in MultiplexLogHandler.metadata (#530)

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 <awais@airtechsolutions.pk>
Co-authored-by: awaisdev5765 <awaisdev5765@users.noreply.github.com>
Co-authored-by: Vladimir Kukushkin <kukushechkin@apple.com>
This commit is contained in:
authored and GitHub committed 2026-11-03 10:57:38 +00:00
1 parent 209524e4da
commit e3da040787
2 files changed
+15 -19

No files matched your search

@@ -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
+9 -12
View File
@@ -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",
]
)
}