credit an image from reddit.

Easy logger (URLSession)

Khwan Siricharoenporn
te<h @TDG
Published in
2 min readSep 25, 2020

--

Hi Today i have the best trick to debug the result from API service. I just want to share an easy solution to implement it. Because when i need to see the result. I must use the breakpoint of Xcode, but I think it would waste a lot of time if your computer is slow. The following by.

Section

  1. Create NetworkLogger
  2. Request
  3. Response

Create NetworkLogger

Create a class by using the name NetworkLogger.

class NetworkLogger {}

Request

When you call the API you can put the URLRequest into the logger. You’ll get the result from this log by you can see it in the console on the right side of Xcode.

static func log(request: URLRequest) {
print("\n - - - - - - - - - - OUTGOING - - - - - - - - - - \n")
defer { print("\n - - - - - - - - - - END - - - - - - - - - - \n") }
let urlAsString = request.url?.absoluteString ?? ""
let urlComponents = URLComponents(string: urlAsString)
let method = request.httpMethod != nil ? "\(request.httpMethod ?? "")" : ""
let path = "\(urlComponents?.path ?? "")"
let query = "\(urlComponents?.query ?? "")"
let host = "\(urlComponents?.host ?? "")"
var output = """
\(urlAsString) \n\n
\(method) \(path)?\(query) HTTP/1.1 \n
HOST: \(host)\n
"""
for (key,value) in request.allHTTPHeaderFields ?? [:] {
output += "\(key): \(value) \n"
}
if let body = request.httpBody {
output += "\n \(String(data: body, encoding: .utf8) ?? "")"
}
print(output)
}

Response

When you receive the API result or json string you can put the HTTPURLResponse, Data, Error into the logger. You’ll get the result from this log by you can see it in the console on the right side of Xcode.

static func log(response: HTTPURLResponse?, data: Data?, error: Error?) {
print("\n - - - - - - - - - - INCOMMING - - - - - - - - - - \n")
defer { print("\n - - - - - - - - - - END - - - - - - - - - - \n") }
let urlString = response?.url?.absoluteString
let components = NSURLComponents(string: urlString ?? "")
let path = "\(components?.path ?? "")"
let query = "\(components?.query ?? "")"
var output = "" if let urlString = urlString {
output += "\(urlString)"
output += "\n\n"
}
if let statusCode = response?.statusCode {
output += "HTTP \(statusCode) \(path)?\(query)\n"
}
if let host = components?.host {
output += "Host: \(host)\n"
}
for (key, value) in response?.allHeaderFields ?? [:] {
output += "\(key): \(value)\n"
}
if let body = data {
output += "\n\(String(data: body, encoding: .utf8) ?? "")\n"
}
if error != nil {
output += "\nError: \(error!.localizedDescription)\n"
}
print(output)
}

This is the basic solution for developers that are too lazy to debug. I think this way may help you to better programming. The static function will manage your application memory. Don’t worry if it is used often in your application.

Thank you~~

--

--

Khwan Siricharoenporn
te<h @TDG

iOS Developer @Central Group. Interested about core code, low-level, design pattern and architecture.