awesome-ios-developer/Helper/Network Layer/Services/NetworkLogger.swift
2021-05-03 01:34:53 -05:00

41 lines
1.3 KiB
Swift

//
// NetworkLogger.swift
// MapDemoApp
//
// Created by JungpyoHong on 4/25/21.
//
import Foundation
class NetworkLogger {
static func log(request: URLRequest) {
print("\n - - - - - - - - - - OUTGOING - - - - - - - - - - \n")
defer { print("\n - - - - - - - - - - END - - - - - - - - - - \n") }
let urlAsString = request.url?.absoluteString ?? ""
let urlComponents = NSURLComponents(string: urlAsString)
let method = request.httpMethod != nil ? "\(request.httpMethod ?? "")" : ""
let path = "\(urlComponents?.path ?? "")"
let query = "\(urlComponents?.query ?? "")"
let host = "\(urlComponents?.host ?? "")"
var logOutput = """
\(urlAsString) \n\n
\(method) \(path)?\(query) HTTP/1.1 \n
HOST: \(host)\n
"""
for (key,value) in request.allHTTPHeaderFields ?? [:] {
logOutput += "\(key): \(value) \n"
}
if let body = request.httpBody {
logOutput += "\n \(NSString(data: body, encoding: String.Encoding.utf8.rawValue) ?? "")"
}
print(logOutput)
}
static func log(response: URLResponse) {}
}