Skip to content

Commit

Permalink
Add pretty printed logs for the data
Browse files Browse the repository at this point in the history
  • Loading branch information
mdb1 committed May 28, 2023
1 parent a516254 commit b7fb577
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 52 deletions.
68 changes: 38 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,37 @@ HTTPClient.shared.networkLogger = .init(
```swift
🛜 ===> Network Request started:
3 key/value pairs
(2 elements)
- key: "Request\'s Internal Id"
- value: "[CA5C3]"
(2 elements)
- key: "URL"
- value: "https://catfact.ninja/fact/?"
(2 elements)
- key: "HTTP Method"
- value: "GET"
(2 elements)
- key: "Request\'s Internal Id"
- value: "[3E786]"
🛜 <==== Network Response received:
3 key/value pairs
(2 elements)
- key: "Status Code"
- value: "200"
(2 elements)
- key: "Request\'s Internal Id"
- value: "[3E786]"
- value: "[CA5C3]"
(2 elements)
- key: "URL"
- value: "https://catfact.ninja/fact/?"
(2 elements)
- key: "Status Code"
- value: "200"
==> JSON Decoding start:
CoreNetworkingTests.CatFact
- fact: "A cat\'s brain is more similar to a man\'s brain than that of a dog."
- length: 66
- fact: "Cats are now Britain\'s favourite pet: there are 7.7 million cats as opposed to 6.6 million dogs."
- length: 96
ℹ️ Additional Info:
2 key/value pairs
(2 elements)
- key: "Expected Model"
- value: "CatFact"
(2 elements)
- key: "UTF8 - String"
- value: "{\"fact\":\"A cat\'s brain is more similar to a man\'s brain than that of a dog.\",\"length\":66}"
ℹ️ 🔍 Expected Model: CatFact
ℹ️ 📝 Pretty Printed JSON:
{
"fact" : "Cats are now Britain's favourite pet: there are 7.7 million cats as opposed to 6.6 million dogs.",
"length" : 96
}
<== JSON Decoding end.
🏁 <==== Network Request finished.
```
Expand All @@ -70,36 +69,45 @@ HTTPClient.shared.networkLogger = .init(
```swift
🛜 ===> Network Request started:
3 key/value pairs
(2 elements)
- key: "Request\'s Internal Id"
- value: "[1C57E]"
(2 elements)
- key: "HTTP Method"
- value: "GET"
(2 elements)
- key: "Request\'s Internal Id"
- value: "[79B5E]"
(2 elements)
- key: "URL"
- value: "https://catfact.ninja/facts/?"
🛜 <==== Network Response received:
3 key/value pairs
(2 elements)
- key: "Request\'s Internal Id"
- value: "[1C57E]"
(2 elements)
- key: "URL"
- value: "https://catfact.ninja/facts/?"
- value: "[79B5E]"
(2 elements)
- key: "Status Code"
- value: "200"
(2 elements)
- key: "URL"
- value: "https://catfact.ninja/facts/?"
==> JSON Decoding issue start:
Error description: Key 'CodingKeys(stringValue: "fact", intValue: nil)' not found
ℹ️ Additional Info:
3 key/value pairs
(2 elements)
- key: "UTF8 - String"
- value: "{\"current_page\":1,\"data\":[{\"fact\":\"Unlike dogs, cats do not have a sweet tooth. Scientists believe this is due to a mutation in a key taste receptor.\",\"length\":114},{\"fact\":\"-}"
(2 elements)
- key: "Expected Model"
- value: "CatFact"
ℹ️ 🔍 Expected Model: CatFact
ℹ️ 📝 Pretty Printed JSON:
{
"last_page_url" : "https:\/\/catfact.ninja\/facts?page=34",
"prev_page_url" : null,
"from" : 1,
"total" : 332,
"path" : "https:\/\/catfact.ninja\/facts",
"first_page_url" : "https:\/\/catfact.ninja\/facts?page=1",
"last_page" : 34,
"next_page_url" : "https:\/\/catfact.ninja\/facts?page=2",
"current_page" : 1,
"per_page" : 10,
"to" : 10
}
1 key/value pair
(2 elements)
- key: "Context"
- value: "No value associated with key CodingKeys(stringValue: \"fact\", intValue: nil) (\"fact\")."
Expand Down
25 changes: 25 additions & 0 deletions Sources/CoreNetworking/Extension/Data+PrettyPrinted.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Data+PrettyPrinted.swift
//
//
// Created by Manu on 28/05/2023.
//

import Foundation

extension Data {
var prettyPrintedJSONString: NSString {
guard let object = try? JSONSerialization.jsonObject(
with: self,
options: []
), let data = try? JSONSerialization.data(
withJSONObject: object,
options: [.prettyPrinted]
), let prettyPrintedString = NSString(
data: data,
encoding: String.Encoding.utf8.rawValue
) else { return "" }

return prettyPrintedString
}
}
26 changes: 26 additions & 0 deletions Sources/CoreNetworking/Extension/Request+Properties.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Request+Properties.swift
//
//
// Created by Manu on 28/05/2023.
//

import Foundation

extension Request {
var logProperties: [String: Any] {
var logProperties: [String: Any] = [:]
logProperties["Request's Internal Id"] = internalId
if let url = urlRequest.url {
logProperties["URL"] = url.absoluteString
}
logProperties["HTTP Method"] = urlRequest.httpMethod
if let headers = urlRequest.allHTTPHeaderFields, headers.count > 0 {
logProperties["HTTP Headers"] = headers
}
if let body = urlRequest.httpBody {
logProperties["HTTP Body"] = String(data: body, encoding: .utf8)
}
return logProperties
}
}
12 changes: 6 additions & 6 deletions Sources/CoreNetworking/NetworkLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,13 @@ public struct NetworkLogger {
switch configuration {
case let .verbose(_, logResponses):
guard logResponses else { return }
var logProperties: [String: String] = [:]
logProperties["UTF8 - String"] = String(data: data, encoding: .utf8)
logProperties["Expected Model"] = "\(type)"

print("✅ ==> JSON Decoding start:")
dump(model)
print("ℹ️ Additional Info:")
dump(logProperties)
print("ℹ️ 🔍 Expected Model: \(type)")
print("ℹ️ 📝 Pretty Printed JSON:")
print(data.prettyPrintedJSONString)
print("✅ <== JSON Decoding end.")
case .quiet:
return
Expand All @@ -78,8 +77,6 @@ public struct NetworkLogger {
guard logResponses else { return }
var errorDescription: String = ""
var logProperties: [String: String] = [:]
logProperties["UTF8 - String"] = String(data: data, encoding: .utf8)
logProperties["Expected Model"] = "\(type)"

if let decodingError = error as? DecodingError {
switch decodingError {
Expand Down Expand Up @@ -108,6 +105,9 @@ public struct NetworkLogger {
print("❌ ==> JSON Decoding issue start:")
print("Error description: \(errorDescription)")
print("ℹ️ Additional Info:")
print("ℹ️ 🔍 Expected Model: \(type)")
print("ℹ️ 📝 Pretty Printed JSON:")
print(data.prettyPrintedJSONString)
dump(logProperties)
print("❌ <== JSON Decoding issue end.")
case .quiet:
Expand Down
16 changes: 0 additions & 16 deletions Sources/CoreNetworking/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,6 @@ public struct Request {
request.httpMethod = method.name
return request
}

var logProperties: [String: Any] {
var logProperties: [String: Any] = [:]
logProperties["Request's Internal Id"] = internalId
if let url = urlRequest.url {
logProperties["URL"] = url.absoluteString
}
logProperties["HTTP Method"] = urlRequest.httpMethod
if let headers = urlRequest.allHTTPHeaderFields, headers.count > 0 {
logProperties["HTTP Headers"] = headers
}
if let body = urlRequest.httpBody {
logProperties["HTTP Body"] = String(data: body, encoding: .utf8)
}
return logProperties
}
}

public extension Request {
Expand Down

0 comments on commit b7fb577

Please sign in to comment.