Gitdi地址:https://github.com/peilinghui/AFNetworkActivityLogger
用法:
[[AFNetworkActivityLogger sharedLogger] setLevel:AFLoggerLevelDebug];
//开启日志
[[AFNetworkActivityLogger sharedLogger] startLogging];
看了一下,也不难就一个.h一个.m。
- 在.h中
用枚举定义了Off,Debug,Info,Warn,还有Error.
一个类方法sharedLogger.两个实例方法startLogging和stopLogging; - 在.m中
先导入#import <AFNetworking/AFURLSessionManager.h>
,主要是检测网络请求。
先定义static变量NSURLRequest,NSError,NSURLResponse(Foundation框架中的)
在实现中,类方法就是
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedLogger = [[self alloc] init];
});```
在实例方法中就是
-startLogging中addObserve两个@selector,一个是networkReqeustDidStart,一个是networkRequestDidFinish。
-stopLogging中removeObserve。
3. 实现两个@selector
a.networkReqeustDidStart
用到了runtime中的设置属性 objc_setAssociatedObject,主要是是可以在switch中的Debug模式下加入代码来实现。比如:
case TDFLoggerLevelDebug: {
NSMutableString *commandLineString = [@"http --form " mutableCopy];
[commandLineString appendFormat:@"%@ '%@' ", request.HTTPMethod, [[request URL] absoluteString]];
[request.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
[commandLineString appendFormat:@"'%@':'%@' ", key, obj];
}];
if (body.length) {
NSArray *parts = [body componentsSeparatedByString:@"&"];
[parts enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSArray *pair = [obj componentsSeparatedByString:@"="];
NSString *key = nil;
if ([pair.firstObject respondsToSelector:@selector(stringByRemovingPercentEncoding)]) {
key = [pair.firstObject stringByRemovingPercentEncoding];
}else {
key = [pair.firstObject stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
NSString *value = nil;
if ([pair.lastObject respondsToSelector:@selector(stringByRemovingPercentEncoding)]) {
value = [pair.lastObject stringByRemovingPercentEncoding];
}else {
value = [pair.lastObject stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
value = [value stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"];
[commandLineString appendFormat:@"'%@'=$'%@' ", key, value];
}];
}
NSLog(@"%@", commandLineString);
}
break;
就可以在程序运行的时候调用服务器提供的接口,打印出信息:比如:
`http --form POST http://10.1.5.109:8080/boss-api//bill/v1/get_payment_record_by_day format='json' find_date='2016-07-21' pay_type='1' session_key='100008999262778ea3c4ffd4efed2e80c68fa2f9a7da33' page='1' sign='b233d533da2379d2fa0bd91c852922e8' page_size='20'`
b.networkRequestDidFinish
用官方的就行,还有在添加:`id responseObject = notification.userInfo[AFNetworkingTaskDidCompleteSerializedResponseKey];`
4.写个单元测试测试一下
-
(void)testLogger {
XCTestExpectation *expectcation = [self expectationWithDescription:@"log test"];AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:@"http://httpbin.org/post" parameters:@{
@"key1" : @"va=lue1",
@"key2" : @"{"key" : value}",
@"key3" : @"valu''''''''''e3",
@"key4" : @"value4",
}progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[expectcation fulfill];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[expectcation fulfill];
}];[self waitForExpectationsWithTimeout:15.0f handler:^(NSError * _Nullable error) {
}];
}
网友评论