Skip to content

Commit

Permalink
wip logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoughjr committed Dec 28, 2024
1 parent ff43cdf commit 1a58637
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions Sources/Request/Request/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,30 @@ extension AnyRequest {
let conf = session.configuration
return """
\(request.httpMethod) \(request.url?.absoluteString ?? "")
Headers: \(request.allHTTPHeaderFields)
Body: \(request.httpBody)
Headers: \(Json(request.allHTTPHeaderFields).stringified ?? "")
Body: \(request.httpBody?.prettyJSON() ?? "")
Config: \(conf)
"""

}
}

extension Data {
func toString() -> String {
return String(data:self, encoding: .utf8) ?? ""
}
func prettyJSON() -> String {
do {
let json = try JSONSerialization.jsonObject(with: self, options: [])
let data = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
guard let jsonString = String(data: data, encoding: .utf8) else {
print("Inavlid data")
return ""
}
return jsonString
} catch {
print("Data+prettyJSON | Error: \(error.localizedDescription)")
}
return ""
}
}

0 comments on commit 1a58637

Please sign in to comment.