Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add withMetadata() convenience function for adding multiple metadata k/v pairs to a Logger #322

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ extension Logger {
}
}

/// Convenience function for merging multiple metadata items into a Logger's existing metadata.
///
/// - parameters:
/// - metadata: The metadata to merge into a Logger's existing metadata.
///
/// - note: Logging metadata behaves as a value that means a change to the logging metadata will only affect the
/// very `Logger` it was changed on.
@inlinable
public mutating func mergeMetadata(_ metadata: Logger.Metadata) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the uniquingKeysWith parameter as well à la Dictionary?

metadata.forEach { (key, value) in
self.handler[metadataKey: key] = value
}
Comment on lines +178 to +180
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect in many (all?) handler implementations this will acquire and release a lock for each value we update. Can we call merge directly on the handler.metadata to avoid this? i.e. self.handler.metadata.merge(metadata)

}

/// Get or set the log level configured for this `Logger`.
///
/// - note: `Logger`s treat `logLevel` as a value. This means that a change in `logLevel` will only affect this
Expand Down
53 changes: 53 additions & 0 deletions Tests/LoggingTests/LoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,59 @@ class LoggingTest: XCTestCase {
"nested-list": ["l1str", ["l2str1", "l2str2"]]])
}

func testMergeMetadata() {
let testLogging = TestLogging()
LoggingSystem.bootstrapInternal { testLogging.make(label: $0) }

var logger = Logger(label: "\(#function)")
let metadata: Logger.Metadata = [
"foo": ["bar", "buz"],
"empty-list": [],
"nested-list": ["l1str", ["l2str1", "l2str2"]],
]
logger.mergeMetadata(metadata)
logger.info("first log")
testLogging.history.assertExist(level: .info,
message: "first log",
metadata: ["foo": ["bar", "buz"],
"empty-list": [],
"nested-list": ["l1str", ["l2str1", "l2str2"]]])

// Non-overlapping metadata key-value pairs should be added without affecting existing metadata,
// while overlapping metadata key-value pairs should update the existing key's value.
logger.mergeMetadata([
"foo": ["bar"], // drops "buz" for existing key value
"newkey": "newvalue1" // adds new key-value pair
])
logger.info("second log")
testLogging.history.assertExist(level: .info,
message: "second log",
metadata: ["foo": ["bar"],
"empty-list": [],
"nested-list": ["l1str", ["l2str1", "l2str2"]],
"newkey": "newvalue1"])

// Finally, if new metadata with overlapping keys is added more than once, the
// latest value should be the one set in the Logger metadata.
logger.mergeMetadata([
"foo": [],
"newkey": "newvalue2"
])
logger.mergeMetadata([
"foo": "a new type for this value",
"newkey": "newvalue3"
])
logger.info("third log")
testLogging.history.assertExist(level: .info,
message: "third log",