如何使用Alamofire记录每个请求/响应? [英] How can I log each request/response using Alamofire?
问题描述
我知道可打印,调试打印和输出(cURL),但是他们不是我正在寻找的东西。
这样的东西可能是你要找的:
扩展请求{
public func debugLog() - >自己{
#if DEBUG
debugPrint(self)
#endif
return self
}
}
用法:
Alamofire.request ,http://httpbin.org/get,参数:[foo:bar])
.debugLog()
.response {...}
如果要打印所有响应,可以编写自己的响应方法,类似于本教程顶部的responseObject()方法:
http://www.raywenderlich.com/ 87595 / intermediate-alamofire-tutorial
[更新:根据@trauzti的请求下面添加。]
下面是如何使用responseObject()方法,以便在每个请求中打印输出。
警告讲师:我避风港不要亲自测试这个代码,并且可能会在生产中做出不同的选择。这简单地显示了Wenderlich教程代码如何可以包括调试日志记录。还要注意:由于本教程是Swift 2.0之前的版本,所以我使用了旧的println()而不是print()。
@objc public protocol ResponseObjectSerializable {
pre>
init(response:NSHTTPURLResponse,representation:AnyObject)
}
extension Alamofire.Request {
public func responseObject< T:ResponseObjectSerializable> ;(completionHandler:(NSURLRequest,NSHTTPURLResponse ?, T?NSError?) - > Void) - >自己{
let serializer:Serializer = {(request,response,data)in
#if DEBUG
println(Request:\(request.URL))
#endif
让JSONSerializer = Request.JSONResponseSerializer(options:.AllowFragments)
let(JSON:AnyObject?,serializationError)= JSONSerializer(请求,响应,数据)
如果回复!= nil&&& JSON!= nil {
#if DEBUG
println(Response:)
debugPrint(JSON)
#endif
return(T(response :$!$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b b b b b b b b b b b b b b b b b b b b b b b b b b b b b
#endif
return(nil,serializationError)
}
}
返回响应(serializer:serializer,completionHandler:{(request,response,object ,错误)
completionHandler(请求,响应,对象为?T,错误)
})
}
}
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.
解决方案Something like this might be what you were looking for:
extension Request { public func debugLog() -> Self { #if DEBUG debugPrint(self) #endif return self } }
Usage:
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"]) .debugLog() .response {…}
If you want to print all responses, you could write your own response method, similar to the responseObject() method at the top of this tutorial:
http://www.raywenderlich.com/87595/intermediate-alamofire-tutorial
[Update: added below per the request from @trauzti.]
Here's how one might do the responseObject() approach in order to print output on every request.
Caveat lector: I haven't personally tested this code, and would probably make different choices in production. This simply shows how the Wenderlich tutorial code can include debug logging. Also note: since the tutorial is pre-Swift 2.0, I've used the old println() instead of print().
@objc public protocol ResponseObjectSerializable { init(response: NSHTTPURLResponse, representation: AnyObject) } extension Alamofire.Request { public func responseObject<T: ResponseObjectSerializable>(completionHandler: (NSURLRequest, NSHTTPURLResponse?, T?, NSError?) -> Void) -> Self { let serializer: Serializer = { (request, response, data) in #if DEBUG println("Request: \(request.URL)") #endif let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments) let (JSON: AnyObject?, serializationError) = JSONSerializer(request, response, data) if response != nil && JSON != nil { #if DEBUG println("Response:") debugPrint(JSON) #endif return (T(response: response!, representation: JSON!), nil) } else { #if DEBUG println("Failed Serialization:") debugPrint(serializationError) #endif return (nil, serializationError) } } return response(serializer: serializer, completionHandler: { (request, response, object, error) in completionHandler(request, response, object as? T, error) }) } }
这篇关于如何使用Alamofire记录每个请求/响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!