How can I log each request/response using Alamofire?

前端 未结 8 1435
迷失自我
迷失自我 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 16:28

    Adding to above answer for Alamofire 4.0+ Swift 3

    extension DataRequest {        
            public func LogRequest() -> Self {
            //Your logic for logging
            return self
        }
    }
    

    When Requesting

    Alamofire.request(requestUrl, method: .post, parameters: parameter, encoding: JSONEncoding.default)
                .LogRequest()
                .responseJSON { response in
                //Do your thing
                }
    

    If you want to cancel the request in any case(which was something I wanted) you can self.cancel() anywhere before you return self

提交回复
热议问题