Metadata shall be machine readable (see SwiftLog/Structured Logging) and the previous metadata serialization erased the nesting structure. The JSON output ensures that the metadata can be quickly analyzed by either a copy and paste into a JSON editor or a small CLI pipeline. See https://swiftpackageindex.com/apple/swift-log/1.10.1/documentation/logging/002-structuredlogging for more information on metadata.
42 lines
1.2 KiB
Swift
42 lines
1.2 KiB
Swift
import Logging
|
|
|
|
extension Logging.Logger.Metadata {
|
|
public func asJSON() -> String {
|
|
return Self.asJSON(self)
|
|
}
|
|
|
|
private static func asJSON(_ metadata: Logging.Logger.Metadata) -> String {
|
|
var outputParts: [String: String] = [:]
|
|
for (key, value) in metadata {
|
|
let jsonKey = Self.escapeForJSON(key)
|
|
outputParts[jsonKey] = Self.asJSON(value)
|
|
}
|
|
return "{" + outputParts.map { "\"\($0)\": \($1)" }.joined(separator: ", ") + "}"
|
|
}
|
|
|
|
private static func asJSON(_ metadata: [Logging.Logger.MetadataValue]) -> String {
|
|
var outputParts: [String] = []
|
|
for item in metadata {
|
|
outputParts.append(Self.asJSON(item))
|
|
}
|
|
return "[" + outputParts.joined(separator: ", ") + "]"
|
|
}
|
|
|
|
private static func asJSON(_ metadata: Logging.Logger.MetadataValue) -> String {
|
|
switch metadata {
|
|
case .dictionary(let subvalues):
|
|
return Self.asJSON(subvalues)
|
|
case .array(let subvalues):
|
|
return Self.asJSON(subvalues)
|
|
case .string(let subvalue):
|
|
return "\"" + Self.escapeForJSON(subvalue) + "\""
|
|
case .stringConvertible(let subvalue):
|
|
return "\"" + Self.escapeForJSON("\(subvalue)") + "\""
|
|
}
|
|
}
|
|
|
|
private static func escapeForJSON(_ data: String) -> String {
|
|
return data.replacingOccurrences(of: "\"", with: "\\\"")
|
|
}
|
|
}
|