-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: NetworkManager init 기본값 제거 * feat: Network Logger 구현
- Loading branch information
Showing
5 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
Projects/Core/Core/Sources/Network/Common/NetworkLogger.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Foundation | ||
import OSLog | ||
|
||
import Alamofire | ||
|
||
final class NetworkLogger: EventMonitor { | ||
let queue = DispatchQueue(label: "NetworkLogger") | ||
|
||
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "", category: "NetworkLogger") | ||
|
||
func request<Value>(_ request: DataRequest, didParseResponse response: DataResponse<Value, AFError>) where Value : Sendable { | ||
let statusCode = response.response?.statusCode ?? 0 | ||
var log = (200..<300) ~= statusCode ? "OK" : "Fail" | ||
log.append(" ") | ||
|
||
log.append(request.description) | ||
log.append("\n\n") | ||
|
||
log.append("------------------- Request --------------------------\n\n") | ||
|
||
log.append("URL: \(request.request?.url?.absoluteString ?? "")\n") | ||
log.append("Method: \(request.request?.httpMethod ?? "")\n") | ||
log.append("Headers: \(request.request?.allHTTPHeaderFields ?? [:])\n") | ||
log.append("Authorization: \(request.request?.headers["Authorization"] ?? "None")\n") | ||
log.append("Body: \(request.request?.httpBody?.toPrettyPrintedString ?? "None")\n\n") | ||
|
||
log.append("------------------- Response --------------------------\n\n") | ||
|
||
log.append("\(response.data?.toPrettyPrintedString ?? "None")") | ||
|
||
logger.log("\(log)") | ||
} | ||
} | ||
|
||
private extension Data { | ||
var toPrettyPrintedString: String? { | ||
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 nil } | ||
return prettyPrintedString as String | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters