Skip to content

Commit ebdfdcf

Browse files
weissiktoso
authored andcommitted
better documentation around MetadataValue being expressible by literals (#85)
* better documentation around MetadataValue being expressible by literals Motivation: In many examples, I could see that users seem to construct metadata values using the explicit enum cases `.string("foo")`, `.array(...)`, as well as `.dictionary(...)`. It's much more readable and easier to type to just use `"foo"`, `["bar", "buz"]`, and `["qux": "quux"]` and rely on the `ExpressibleBy...Literal` conformances. Modifications: Add the docs and examples. Result: Hopefully, users will read the docs and write more idiomatic code. * Update Logging.swift
1 parent 2637350 commit ebdfdcf

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Sources/Logging/Logging.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,17 +288,38 @@ extension Logger {
288288
public typealias Metadata = [String: MetadataValue]
289289

290290
/// A logging metadata value. `Logger.MetadataValue` is string, array, and dictionary literal convertible.
291+
///
292+
/// `MetadataValue` provides convenient conformances to `ExpressibleByStringInterpolation`,
293+
/// `ExpressibleByStringLiteral`, `ExpressibleByArrayLiteral`, and `ExpressibleByDictionaryLiteral` which means
294+
/// that when constructing `MetadataValue`s you should default to using Swift's usual literals.
295+
///
296+
/// Examples:
297+
/// - prefer `logger.info("user logged in", metadata: ["user-id": "\(user.id)"])` over
298+
/// `..., metadata: ["user-id": .string(user.id.description)])`
299+
/// - prefer `logger.info("user selected colours", metadata: ["colors": ["\(user.topColor)", "\(user.secondColor)"]])`
300+
/// over `..., metadata: ["colors": .array([.string("\(user.topColor)"), .string("\(user.secondColor)")])`
301+
/// - prefer `logger.info("nested info", metadata: ["nested": ["fave-numbers": ["\(1)", "\(2)", "\(3)"], "foo": "bar"]])`
302+
/// over `..., metadata: ["nested": .dictionary(["fave-numbers": ...])])`
291303
public enum MetadataValue {
292304
/// A metadata value which is a `String`.
305+
///
306+
/// Because `MetadataValue` implements `ExpressibleByStringInterpolation`, and `ExpressibleByStringLiteral`,
307+
/// you don't need to type `.string(someType.description)` you can use the string interpolation `"\(someType)"`.
293308
case string(String)
294309

295310
/// A metadata value which is some `CustomStringConvertible`.
296311
case stringConvertible(CustomStringConvertible)
297312

298313
/// A metadata value which is a dictionary from `String` to `Logger.MetadataValue`.
314+
///
315+
/// Because `MetadataValue` implements `ExpressibleByDictionaryLiteral`, you don't need to type
316+
/// `.dictionary(["foo": .string("bar \(buz)")])`, you can just use the more natural `["foo": "bar \(buz)"]`.
299317
case dictionary(Metadata)
300318

301319
/// A metadata value which is an array of `Logger.MetadataValue`s.
320+
///
321+
/// Because `MetadataValue` implements `ExpressibleByArrayLiteral`, you don't need to type
322+
/// `.array([.string("foo"), .string("bar \(buz)")])`, you can just use the more natural `["foo", "bar \(buz)"]`.
302323
case array([Metadata.Value])
303324
}
304325

0 commit comments

Comments
 (0)