print Vs debugPrint

Abhishek Bedi
2 min readSep 23, 2017

--

print is our favourite logging mechanism. In fact in Swift, we use print everywhere (if not using any custom logger). But recently I came across another logging keyword called debugPrint. Lets see what it is.

debugPrint — Once can easily get into the misconception that debugPrint is a print for debug configuration and wont log for release configuration. But thats not the case.

From Apple’s documentation: debugPrint

Writes the textual representations of the given items most suitable for debugging into the standard output.

But there is more to it than just printing simple values.

The Network Response Test

Almost every app consumes webservice and response logging is the most basic thing we do. Lets see how debugPrint helps us over the print command.

The Request:

Consider we hit iTunes Search Api to get some json back.

let urlReq = URLRequest(url: URL(string: "https://itunes.apple.com/search?term=jack+johnson&limit=1")!)
Alamofire.request(urlReq).responseJSON { (data) in
print(data)
print("\n\n\n\n\n\n\n\n\n")
debugPrint(data)
}

The Response:

For the sake of this article I have kept only a few json keys

For print:

For debugPrint:

Clearly, with debugPrint besides (the response json) , we have the following:

  • The response gives information about the request also. Thats nice! as we don’t need to print the request anymore.
  • Response Headers
  • Data transferred in bytes.
  • Timeline info highlighting the Total Duration for the return trip and Network Latency.

No wonder, this is good information especially the Network Latency without using any third party tools.

The Pros of debugPrint:

  • Useful information pertaining to debugging the application
  • Extra information regarding the network calls
  • Works in release builds also

The Cons of debugPrint:

  • Justified usage causes no harms
  • So much extra information in the console sometimes is not really needed as it slows the debugging.

If you have any questions or comments on this article, please write below!

--

--

Abhishek Bedi

The Power Of Imagination 🤔 … You Can’t Even Imagine 😇