Skip to content

Commit

Permalink
[D-1] Network Logger 구현 (539) (#83)
Browse files Browse the repository at this point in the history
* chore: NetworkManager init 기본값 제거

* feat: Network Logger 구현
  • Loading branch information
Siwon-L authored Dec 12, 2024
1 parent 5e5369e commit 641e525
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
42 changes: 42 additions & 0 deletions Projects/Core/Core/Sources/Network/Common/NetworkLogger.swift
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,15 +58,15 @@ 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
)
.validateTokenExpire()

default:
dataRequest = AF.request(
dataRequest = session.request(
target,
interceptor: tokenIntercepter
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public final class SignDIContainer {

public init(
localStorage: LocalStorageInterface = LocalStorage(),
networkManager: NetworkManagerInterfacae = NetworkManager()
networkManager: NetworkManagerInterfacae
) {
self.localStorage = localStorage
self.networkManager = networkManager
Expand Down

0 comments on commit 641e525

Please sign in to comment.