-
-
Notifications
You must be signed in to change notification settings - Fork 624
/
NetworkLogger.swift
executable file
·39 lines (32 loc) · 1.52 KB
/
NetworkLogger.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import Foundation
import Moya
import Result
/// Logs network activity (outgoing requests and incoming responses).
class NetworkLogger: PluginType {
typealias Comparison = (TargetType) -> Bool
let whitelist: Comparison
let blacklist: Comparison
init(whitelist: @escaping Comparison = { _ -> Bool in return true }, blacklist: @escaping Comparison = { _ -> Bool in return true }) {
self.whitelist = whitelist
self.blacklist = blacklist
}
func willSendRequest(_ request: RequestType, target: TargetType) {
// If the target is in the blacklist, don't log it.
guard blacklist(target) == false else { return }
logger.log("Sending request: \(request.request?.url?.absoluteString ?? String())")
}
func didReceiveResponse(_ result: Result<Moya.Response, Moya.Error>, target: TargetType) {
// If the target is in the blacklist, don't log it.
guard blacklist(target) == false else { return }
switch result {
case .success(let response):
if 200..<400 ~= (response.statusCode ) && whitelist(target) == false {
// If the status code is OK, and if it's not in our whitelist, then don't worry about logging its response body.
logger.log("Received response(\(response.statusCode )) from \(response.response?.url?.absoluteString ?? String()).")
}
case .failure(let error):
// Otherwise, log everything.
logger.log("Received networking error: \(error)")
}
}
}