Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log each request/response using Alamofire?

Is there a way to log each request / response using Alamofire (something similar to AFNetworkActivityLogger) ?

I am aware of Printable, DebugPrintable and Output (cURL) but they are not quite what I am looking for.

like image 808
Cosmin Avatar asked Nov 04 '14 13:11

Cosmin


People also ask

What is the advantage of Alamofire?

Alamofire makes developing networking layers easier, faster and much cleaner. Another great benefit of using it is that it can be studied and its code is available. This can help programmers because it's a well-written framework.

Is Alamofire asynchronous?

Everything with Alamofire is asynchronous, which means you'll update the UI in an asynchronous manner: Hide the upload button, and show the progress view and activity view. While the file uploads, you call the progress handler with an updated percent.


1 Answers

There's a sweet little pod for this: https://github.com/konkab/AlamofireNetworkActivityLogger

Add this to your podfile:

pod 'AlamofireNetworkActivityLogger', '~> 2.0' 

In your AppDelegate:

import AlamofireNetworkActivityLogger 

Then in your didFinishLaunchingWithOptions, add this:

NetworkActivityLogger.shared.level = .debug NetworkActivityLogger.shared.startLogging() 

EDIT: I've actually encountered crashes with this in production. To be on the safe side, use "build flags" to only use this in debug, something like this:

#if DEBUG     NetworkActivityLogger.shared.level = .debug     NetworkActivityLogger.shared.startLogging() #endif 
like image 128
ullstrm Avatar answered Oct 23 '22 23:10

ullstrm