From 641e52556b228f899e6a64ba1d457bf10d4465cc Mon Sep 17 00:00:00 2001 From: Safari <91936941+Siwon-L@users.noreply.github.com> Date: Thu, 12 Dec 2024 11:03:22 +0900 Subject: [PATCH] =?UTF-8?q?[D-1]=20Network=20Logger=20=EA=B5=AC=ED=98=84?= =?UTF-8?q?=20(539)=20(#83)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: NetworkManager init 기본값 제거 * feat: Network Logger 구현 --- .../SignAPI/Repository/SignRepository.swift | 2 +- .../Repository/UniversityRepository.swift | 2 +- .../Network/Common/NetworkLogger.swift | 42 +++++++++++++++++++ .../Network/Common/NetworkManager.swift | 8 ++-- .../Scene/Common/SignDIContainer.swift | 2 +- 5 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 Projects/Core/Core/Sources/Network/Common/NetworkLogger.swift diff --git a/Projects/Core/Core/Sources/Network/APIs/SignAPI/Repository/SignRepository.swift b/Projects/Core/Core/Sources/Network/APIs/SignAPI/Repository/SignRepository.swift index 19849730..09401ed5 100644 --- a/Projects/Core/Core/Sources/Network/APIs/SignAPI/Repository/SignRepository.swift +++ b/Projects/Core/Core/Sources/Network/APIs/SignAPI/Repository/SignRepository.swift @@ -22,7 +22,7 @@ public final class SignRepository: SignRepositoryInterface { private let appleAuthManager: AppleAuthManager public init( - networkManager: NetworkManagerInterfacae = NetworkManager(), + networkManager: NetworkManagerInterfacae, localStorage: LocalStorageInterface = LocalStorage(), kakaoAuthManager: KakaoAuthManager = .shared, appleAuthManager: AppleAuthManager = AppleAuthManager() diff --git a/Projects/Core/Core/Sources/Network/APIs/UniversityAPI/Repository/UniversityRepository.swift b/Projects/Core/Core/Sources/Network/APIs/UniversityAPI/Repository/UniversityRepository.swift index c8e72af0..5caa4362 100644 --- a/Projects/Core/Core/Sources/Network/APIs/UniversityAPI/Repository/UniversityRepository.swift +++ b/Projects/Core/Core/Sources/Network/APIs/UniversityAPI/Repository/UniversityRepository.swift @@ -9,7 +9,7 @@ public protocol UniversityRepositoryInterface { public final class UniversityRepository: UniversityRepositoryInterface { private let networkManager: NetworkManagerInterfacae - public init(networkManager: NetworkManagerInterfacae = NetworkManager()) { + public init(networkManager: NetworkManagerInterfacae) { self.networkManager = networkManager } diff --git a/Projects/Core/Core/Sources/Network/Common/NetworkLogger.swift b/Projects/Core/Core/Sources/Network/Common/NetworkLogger.swift new file mode 100644 index 00000000..851519ef --- /dev/null +++ b/Projects/Core/Core/Sources/Network/Common/NetworkLogger.swift @@ -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(_ request: DataRequest, didParseResponse response: DataResponse) 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 + } +} diff --git a/Projects/Core/Core/Sources/Network/Common/NetworkManager.swift b/Projects/Core/Core/Sources/Network/Common/NetworkManager.swift index 3b1ba0fa..78b94f12 100644 --- a/Projects/Core/Core/Sources/Network/Common/NetworkManager.swift +++ b/Projects/Core/Core/Sources/Network/Common/NetworkManager.swift @@ -12,10 +12,12 @@ public final class NetworkManager: NetworkManagerInterfacae { public var tokenIntercepter: TokenRequestIntercepter? + private let session = Session(eventMonitors: [NetworkLogger()]) + public init() {} public func request(target: TargetType) async throws { - let dataResponse = await AF.request(target, interceptor: tokenIntercepter) + let dataResponse = await session.request(target, interceptor: tokenIntercepter) .validateTokenExpire() .serializingData() .response @@ -56,7 +58,7 @@ public final class NetworkManager: NetworkManagerInterfacae { let dataRequest: DataRequest switch target.task { case .upload(let multipartFormData): - dataRequest = AF.upload( + dataRequest = session.upload( multipartFormData: multipartFormData, with: target, interceptor: tokenIntercepter @@ -64,7 +66,7 @@ public final class NetworkManager: NetworkManagerInterfacae { .validateTokenExpire() default: - dataRequest = AF.request( + dataRequest = session.request( target, interceptor: tokenIntercepter ) diff --git a/Projects/Feature/Sign/Sources/Scene/Common/SignDIContainer.swift b/Projects/Feature/Sign/Sources/Scene/Common/SignDIContainer.swift index 06d6c679..7fd8263a 100644 --- a/Projects/Feature/Sign/Sources/Scene/Common/SignDIContainer.swift +++ b/Projects/Feature/Sign/Sources/Scene/Common/SignDIContainer.swift @@ -6,7 +6,7 @@ public final class SignDIContainer { public init( localStorage: LocalStorageInterface = LocalStorage(), - networkManager: NetworkManagerInterfacae = NetworkManager() + networkManager: NetworkManagerInterfacae ) { self.localStorage = localStorage self.networkManager = networkManager