How can I log each request/response using Alamofire?

前端 未结 8 1439
迷失自我
迷失自我 2020-12-23 16:20

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

I am aware of Printable, DebugPrintable and Output (cU

8条回答
  •  情深已故
    2020-12-23 16:28

    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
    

提交回复
热议问题