Skip to content

Commit d99a002

Browse files
authored
[Feat] 네트워킹 로깅 플러그인 구현
* feat: 네트워킹 로깅 플러그인 구현 * refactor: 불필요한 공백, 상수 제거
1 parent 8e6610b commit d99a002

2 files changed

Lines changed: 72 additions & 12 deletions

File tree

Projects/DataSource/Sources/NetworkService/NetworkService.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ final class NetworkService {
1818
plugins = [
1919
TokenInjectionPlugin(),
2020
RefreshTokenPlugin(),
21-
RetryPlugin()]
21+
RetryPlugin(),
22+
BitnagilLoggingPlugin()]
2223
}
2324

2425
func request<T: Decodable>(
@@ -61,11 +62,6 @@ final class NetworkService {
6162

6263
let (data, response) = try await URLSession.shared.data(for: request)
6364

64-
// TODO: - 로깅 로직 수정
65-
if let httpResponse = response as? HTTPURLResponse {
66-
BitnagilLogger.log(logType: .info, message: "응답 코드: \(httpResponse.statusCode)")
67-
}
68-
6965
if withPlugins {
7066
for plugin in plugins {
7167
try await plugin.didReceive(
@@ -79,19 +75,15 @@ final class NetworkService {
7975
else { throw NetworkError.invalidResponse }
8076

8177
guard 200..<300 ~= httpResponse.statusCode else {
82-
BitnagilLogger.log(logType: .error, message: "응답 코드: \(httpResponse.statusCode)")
8378
throw NetworkError.invalidStatusCode(statusCode: httpResponse.statusCode)
8479
}
8580

86-
guard !data.isEmpty
87-
else { throw NetworkError.emptyData }
81+
guard !data.isEmpty else { throw NetworkError.emptyData }
8882

8983
do {
9084
let baseResponse = try decoder.decode(BaseResponse<T>.self, from: data)
91-
BitnagilLogger.log(logType: .info, message: "Server Message: \(baseResponse.message)")
9285

93-
guard let responseDTO = baseResponse.data
94-
else { return nil }
86+
guard let responseDTO = baseResponse.data else { return nil }
9587

9688
return responseDTO
9789
} catch {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// BitnagilLoggingPlugin.swift
3+
// DataSource
4+
//
5+
// Created by 이동현 on 8/3/25.
6+
//
7+
8+
import Foundation
9+
import Shared
10+
11+
struct BitnagilLoggingPlugin: NetworkPlugin {
12+
func willSend(request: URLRequest, endpoint: Endpoint) async throws -> URLRequest {
13+
let urlString = request.url?.absoluteString ?? ""
14+
let method = request.httpMethod ?? ""
15+
let headers = request.allHTTPHeaderFields ?? [:]
16+
let body: String
17+
if let bodyData = request.httpBody {
18+
body = String(data: bodyData, encoding: .utf8) ?? "디코딩 실패"
19+
} else {
20+
body = "내용 없음"
21+
}
22+
23+
BitnagilLogger.log(
24+
logType: .debug,
25+
message: """
26+
REQUEST➡️
27+
- URL: \(urlString)
28+
- Method: \(method)
29+
- Headers: \(headers)
30+
- Body: \(body)
31+
"""
32+
)
33+
return request
34+
}
35+
36+
func didReceive(response: URLResponse, data: Data?, endpoint: Endpoint) async throws {
37+
let urlString = response.url?.absoluteString ?? ""
38+
39+
guard let httpResponse = response as? HTTPURLResponse else {
40+
BitnagilLogger.log(logType: .error, message: "🚨\(urlString)에 대한 response는 HTTPResponse가 아닙니다.🚨")
41+
return
42+
}
43+
44+
let statusCode = httpResponse.statusCode
45+
46+
// code/message 추출
47+
var code: String = ""
48+
var message: String = ""
49+
if
50+
let data = data,
51+
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any]
52+
{
53+
code = json["code"] as? String ?? ""
54+
message = json["message"] as? String ?? ""
55+
}
56+
57+
BitnagilLogger.log(
58+
logType: .debug,
59+
message: """
60+
RESPONSE⬅️
61+
- 요청URL: \(urlString)
62+
- Status: \(statusCode)
63+
- Code: \(code)
64+
- Message: \(message)
65+
"""
66+
)
67+
}
68+
}

0 commit comments

Comments
 (0)