File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -166,6 +166,17 @@ extension Logger {
166
166
}
167
167
}
168
168
169
+ /// Convenience function for adding multiple metadata items to a logger.
170
+ ///
171
+ /// - note: Logging metadata behaves as a value that means a change to the logging metadata will only affect the
172
+ /// very `Logger` it was changed on.
173
+ @inlinable
174
+ public mutating func withMetadata( metadata: Logger . Metadata ) {
175
+ metadata. forEach { ( key, value) in
176
+ self . handler [ metadataKey: key] = value
177
+ }
178
+ }
179
+
169
180
/// Get or set the log level configured for this `Logger`.
170
181
///
171
182
/// - note: `Logger`s treat `logLevel` as a value. This means that a change in `logLevel` will only affect this
Original file line number Diff line number Diff line change @@ -357,6 +357,25 @@ class LoggingTest: XCTestCase {
357
357
" nested-list " : [ " l1str " , [ " l2str1 " , " l2str2 " ] ] ] )
358
358
}
359
359
360
+ func testWithMetadata( ) {
361
+ let testLogging = TestLogging ( )
362
+ LoggingSystem . bootstrapInternal { testLogging. make ( label: $0) }
363
+
364
+ var logger = Logger ( label: " \( #function) " )
365
+ let metadata : Logger . Metadata = [
366
+ " foo " : [ " bar " , " buz " ] ,
367
+ " empty-list " : [ ] ,
368
+ " nested-list " : [ " l1str " , [ " l2str1 " , " l2str2 " ] ] ,
369
+ ]
370
+ logger. withMetadata ( metadata: metadata)
371
+ logger. info ( " hello world! " )
372
+ testLogging. history. assertExist ( level: . info,
373
+ message: " hello world! " ,
374
+ metadata: [ " foo " : [ " bar " , " buz " ] ,
375
+ " empty-list " : [ ] ,
376
+ " nested-list " : [ " l1str " , [ " l2str1 " , " l2str2 " ] ] ] )
377
+ }
378
+
360
379
// Example of custom "box" which may be used to implement "render at most once" semantics
361
380
// Not thread-safe, thus should not be shared across threads.
362
381
internal final class LazyMetadataBox : CustomStringConvertible {
You can’t perform that action at this time.
0 commit comments