mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
This has numerous benefits, most of which center around values which may have expensive to perform custom `description` implementations, e.g. yet are neither a dictionary nor an array. It also allows carrying and thus "delaying" rendering of string representations until they are actually needed. E.g. one can imagine wanting to avoid rendering a string representation of a value if a logger never would log any of the metadata, esp. if the rendering is a heavy process.
This commit is contained in:
1 parent
5c135cd948
commit
b0e59933ac
3 files changed
+52
No files matched your search
@@ -196,6 +196,7 @@ The logging metadata is of type `typealias Logging.Metadata = [String: Metadata.
|
||||
```swift
|
||||
public enum MetadataValue {
|
||||
case string(String)
|
||||
case stringConvertible(CustomStringConvertible)
|
||||
case dictionary(Metadata)
|
||||
case array([Metadata.Value])
|
||||
}
|
||||
|
||||
@@ -115,6 +115,7 @@ extension Logger {
|
||||
|
||||
public enum MetadataValue {
|
||||
case string(String)
|
||||
case stringConvertible(CustomStringConvertible)
|
||||
case dictionary(Metadata)
|
||||
case array([Metadata.Value])
|
||||
}
|
||||
@@ -146,6 +147,8 @@ extension Logger.MetadataValue: Equatable {
|
||||
switch (lhs, rhs) {
|
||||
case (.string(let lhs), .string(let rhs)):
|
||||
return lhs == rhs
|
||||
case (.stringConvertible(let lhs), .stringConvertible(let rhs)):
|
||||
return lhs.description == rhs.description
|
||||
case (.array(let lhs), .array(let rhs)):
|
||||
return lhs == rhs
|
||||
case (.dictionary(let lhs), .dictionary(let rhs)):
|
||||
@@ -295,6 +298,8 @@ extension Logger.MetadataValue: CustomStringConvertible {
|
||||
return list.map { $0.description }.description
|
||||
case .string(let str):
|
||||
return str
|
||||
case .stringConvertible(let repr):
|
||||
return repr.description
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,6 +129,52 @@ class LoggingTest: XCTestCase {
|
||||
"nested-list": ["l1str", ["l2str1", "l2str2"]]])
|
||||
}
|
||||
|
||||
// Example of custom "box" which may be used to implement "render at most once" semantics
|
||||
// Not thread-safe, thus should not be shared across threads.
|
||||
internal class LazyMetadataBox: CustomStringConvertible {
|
||||
private var makeValue: (() -> String)?
|
||||
private var _value: String? = nil
|
||||
|
||||
public init(_ makeValue: @escaping () -> String) {
|
||||
self.makeValue = makeValue
|
||||
}
|
||||
|
||||
/// This allows caching a value in case it is accessed via an by name subscript,
|
||||
// rather than as part of rendering all metadata that a LoggingContext was carrying
|
||||
public var value: String {
|
||||
if let f = self.makeValue {
|
||||
self._value = f()
|
||||
self.makeValue = nil
|
||||
}
|
||||
|
||||
assert(self._value != nil, "_value MUST NOT be nil once `lazyValue` has run.")
|
||||
return self._value!
|
||||
}
|
||||
|
||||
var hasRendered: Bool {
|
||||
return value != nil
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return "\(self.value)"
|
||||
}
|
||||
}
|
||||
|
||||
func testStringConvertibleMetadata() {
|
||||
let testLogging = TestLogging()
|
||||
Logging.bootstrap(testLogging.make)
|
||||
var logger = Logging.make("\(#function)")
|
||||
logger[metadataKey: "foo"] = .stringConvertible("raw-string")
|
||||
let lazyBox = LazyMetadataBox({ "rendered-at-first-use" })
|
||||
logger[metadataKey: "lazy"] = .stringConvertible(lazyBox)
|
||||
logger.info("hello world!")
|
||||
XCTAssertTrue(lazyBox.hasRendered)
|
||||
testLogging.history.assertExist(level: .info,
|
||||
message: "hello world!",
|
||||
metadata: ["foo": .stringConvertible("raw-string"),
|
||||
"lazy": .stringConvertible(LazyMetadataBox({ "rendered-at-first-use" }))])
|
||||
}
|
||||
|
||||
private func dontEvaluateThisString(file: StaticString = #file, line: UInt = #line) -> String {
|
||||
XCTFail("should not have been evaluated", file: file, line: line)
|
||||
return "should not have been evaluated"
|
||||
|
||||
Reference in new issue
Block a user