MetadataValue

public enum MetadataValue

A logging metadata value. Logger.MetadataValue is string, array, and dictionary literal convertible.

MetadataValue provides convenient conformances to ExpressibleByStringInterpolation, ExpressibleByStringLiteral, ExpressibleByArrayLiteral, and ExpressibleByDictionaryLiteral which means that when constructing MetadataValues you should default to using Swift’s usual literals.

Examples:

  • prefer logger.info("user logged in", metadata: ["user-id": "\(user.id)"]) over ..., metadata: ["user-id": .string(user.id.description)])
  • prefer logger.info("user selected colours", metadata: ["colors": ["\(user.topColor)", "\(user.secondColor)"]]) over ..., metadata: ["colors": .array([.string("\(user.topColor)"), .string("\(user.secondColor)")])
  • prefer logger.info("nested info", metadata: ["nested": ["fave-numbers": ["\(1)", "\(2)", "\(3)"], "foo": "bar"]]) over ..., metadata: ["nested": .dictionary(["fave-numbers": ...])])
  • A metadata value which is a String.

    Because MetadataValue implements ExpressibleByStringInterpolation, and ExpressibleByStringLiteral, you don’t need to type .string(someType.description) you can use the string interpolation "\(someType)".

    Declaration

    Swift

    case string(String)
  • A metadata value which is some CustomStringConvertible.

    Declaration

    Swift

    case stringConvertible(CustomStringConvertible)
  • A metadata value which is a dictionary from String to Logger.MetadataValue.

    Because MetadataValue implements ExpressibleByDictionaryLiteral, you don’t need to type .dictionary(["foo": .string("bar \(buz)")]), you can just use the more natural ["foo": "bar \(buz)"].

    Declaration

    Swift

    case dictionary(Metadata)
  • A metadata value which is an array of Logger.MetadataValues.

    Because MetadataValue implements ExpressibleByArrayLiteral, you don’t need to type .array([.string("foo"), .string("bar \(buz)")]), you can just use the more natural ["foo", "bar \(buz)"].

    Declaration

    Swift

    case array([Metadata.Value])