How can I log each request/response using Alamofire?

Cosmin picture Cosmin · Nov 4, 2014 · Viewed 35.3k times · Source

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.

Answer

ullstrm picture ullstrm · May 11, 2017

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