Up next:

Logging and Printing with Alamofire

In this lesson

Alamofire has a couple handy ways to handle logging and printing during runtime. Standard print has overriden the default functionality to include more helpful details about certain Alamofire object and debugPrint has been added for even more of those helpful details! Here’s how you use them.

Kyle's Series

Transcript

Tap on time to skip ahead

0:11

0:11

Let’s talk about logging support in Alamofire. So obviously, we can just print Alamofire objects like this straight to the log. A pretty neat thing implemented into Alamofire is the ability to debugPrint different Alamofire objects to the log. So I’m going to first print the request and then run a debugPrint on the request and it will add both to the console down here and we can compare the differences.

0:40

0:40

But debugPrint is created, probably self explanatory, but just as a way to view more information about the current state of the object getting logged. So I’m going to step over and we’ll see the first standard print statement of the Alamofire request object. It prints the HTTP method in then the URL. Step over one more time for debugPrint in what it does is it actually prints out some of the HTTP headers associated with the request. I didn’t explicitly define any in the code but I’m guessing that Alamofire might have some default ones they create somewhere behind the scenes. That’s basically all there is to these two logging statements. Of course print comes with Swift and then debugPrint was added later.

1:27

1:27

But let’s just go through another example and paste in response to print and debugPrint the response object so I’ll have to rerun the app. And here we are in the response closure of our request, and pressing step over to print in the response to the log. Now what this does is it ends up printing the serialized data from the request, and in this case, I’m serializing some JSON and there’s a lot of JSON. And really this is where the standard print of a response starts immediately and only with the JSON. So there’s no other associated data there.

2:12

2:12

But then if we run a debugPrint on the response, it actually gives us some details of the response such as the start time, the response time, request completed time, a bunch of other times in this whole request thing. It says it ran for 18 seconds but I’m sure that is counting at some point while we were paused because I don’t think that took 18 seconds, it normally doesn’t. You should be able to use debugPrint with any of the Alamofire objects like this. There’s a request, response, and then some of the properties of response as well like response.result or anything like that. But it’s pretty neat just to get a an extra glimpse into some information that you might not always see.