NSLogger的使用
-
用途: 用来监控app的日志,从此摆脱xcode的控制台
mac版界面如下:
mac版qq客户端的日志 -
使用步骤:
- 安装 NSLogger Viewer MAC端的日志分析,通过网络可以将日志传进来 ,默认接受电脑所在局域网发出的所有日志
ps:所以当我安装NSLogger Viewer之后发现它监听到了macQQ的日志,看来macQQ也有使用NSLogger啊,哈哈
- 在项目中加入NSLogger框架,支持pod
- 导入
#import "NSLogger.h"
头文件</br></br>
(接下来是我的一些处理): - 添加一个宏,自由切换xcode控制台和NSLogger
- 安装 NSLogger Viewer MAC端的日志分析,通过网络可以将日志传进来 ,默认接受电脑所在局域网发出的所有日志
// 使用NSLogger打印:取消注释下一行;使用xcode控制台:注释下一行
//#define NSLOGGER_CLIENT
* 导入`NSLogger.h `时进行判断
#ifdef NSLOGGER_CLIENT
#import "NSLogger.h" //使用NSLogger
#else
#ifdef DEBUG
#define LoggerError(level,...) NSLog(__VA_ARGS__)
#define LoggerApp(level, ...) NSLog(__VA_ARGS__)
#define LoggerView(level,...) NSLog(__VA_ARGS__)
#define LoggerService(level,...) NSLog(__VA_ARGS__)
#define LoggerModel(level,...) NSLog(__VA_ARGS__)
#define LoggerData(level,...) NSLog(__VA_ARGS__)
#define LoggerNetwork(level,...) NSLog(__VA_ARGS__)
#define LoggerLocation(level,...) NSLog(__VA_ARGS__)
#define LoggerPush(level,...) NSLog(__VA_ARGS__)
#define LoggerFile(level,...) NSLog(__VA_ARGS__)
#define LoggerSharing(level,...) NSLog(__VA_ARGS__)
#define LoggerAd(level,...) NSLog(__VA_ARGS__)
#else
#define NSLog(...) while(0) {}
#define LoggerError(...) while(0) {}
#define LoggerApp(level, ...) while(0) {}
#define LoggerView(...) while(0) {}
#define LoggerService(...) while(0) {}
#define LoggerModel(...) while(0) {}
#define LoggerData(...) while(0) {}
#define LoggerNetwork(...) while(0) {}
#define LoggerLocation(...) while(0) {}
#define LoggerPush(...) while(0) {}
#define LoggerFile(...) while(0) {}
#define LoggerSharing(...) while(0) {}
#define LoggerAd(...) while(0) {}
#endif
#endif
原理:
在NSLogger.h
文件中定义了如下宏
#define NSLog(...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"NSLog", 0, __VA_ARGS__)
#define LoggerError(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"Error", level, __VA_ARGS__)
#define LoggerApp(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"App", level, __VA_ARGS__)
#define LoggerView(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"View", level, __VA_ARGS__)
#define LoggerService(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"Service", level, __VA_ARGS__)
#define LoggerModel(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"Model", level, __VA_ARGS__)
#define LoggerData(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"Data", level, __VA_ARGS__)
#define LoggerNetwork(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"Network", level, __VA_ARGS__)
#define LoggerLocation(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"Location", level, __VA_ARGS__)
#define LoggerPush(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"Push", level, __VA_ARGS__)
#define LoggerFile(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"File", level, __VA_ARGS__)
#define LoggerSharing(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"Sharing", level, __VA_ARGS__)
#define LoggerAd(level, ...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"Ad and Stat", level, __VA_ARGS__)
其中
#define NSLog(...) LogMessageF(__FILE__, __LINE__, __FUNCTION__, @"NSLog", 0, __VA_ARGS__)```
直接将所有的NSLog替换为了LogMessageF
进入LogMessage发现他是如下的一个方法
void LogMessageF(const char *filename, int lineNumber, const char *functionName, NSString *domain, int level, NSString *format, ...)
{
va_list args;
va_start(args, format);
LogMessageTo_internal(NULL, filename, lineNumber, functionName, domain, level, format, args);
va_end(args);
}
其中`LogMessageTo_internal(NULL, filename, lineNumber, functionName, domain, level, format, args);` 就是向网络发送数据
同时`NSLogger.h `文件中还定义了很多其他的宏,根据命名大概就可以猜到他们的用途吧
ps: 更多功能有待大家去探索,不想写2333~
附一段网络请求单例方法代码(请忽略里面一大串恶心的东西,只是想说明一下NSLogger的使用):
-(void)sendRequest:(NSMutableDictionary *)mutableDic
url:(NSString *)url
progress:( void (^)(NSProgress *uploadProgress))uploadProgressBack
compelete:(void(^)(BOOL success, NSDictionary *responseObject))compelete {
if (manager == nil) {
manager = [AFHTTPSessionManager manager];
}
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json",nil];
[manager.requestSerializer setValue:@"ture" forHTTPHeaderField:@"ifmodified"];
[manager.requestSerializer setValue:@"ios" forHTTPHeaderField:@"app"];
NSMutableDictionary *paramDic = nil;
if(mutableDic) {
paramDic = [[NSMutableDictionary alloc] initWithDictionary:mutableDic];
}
else {
paramDic = [[NSMutableDictionary alloc] init];
}
[paramDic setValue:[[JFUserInfo shareUserInfo] getToken] forKey:kUSERTOKE];
[paramDic setValue:[NSString appVersion] forKey:kCLIENTVERSION];
//使用NSLogger
LoggerService(2,@"%@",url);
LoggerData(2,@"paramDic = %@", paramDic);
[manager POST:url parameters:paramDic progress:^(NSProgress * _Nonnull uploadProgress) {
if (uploadProgressBack) {
uploadProgressBack(uploadProgress);
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//使用NSLogger
LoggerData(3,@"responseObject = %@", responseObject);
NSLog(@"errmsg:%@",(NSDictionary *)responseObject[@"errmsg"]);
NSDictionary *responseDic = (NSDictionary *)responseObject;
NSNumber *code = responseDic[@"errcode"];
if ([code integerValue] == 2000001 || [code integerValue] == -4) {//重新登录
NSLog(@"错误请求:%@",responseDic[@"errmsg"]);
[[AppDelegate sharedAppDelegate] enterLoginUI];
return;
}
if (compelete) {
if ([code integerValue] == 0) {
compelete(YES, responseDic);
}else if ([code integerValue] < 0){
// NSLog(@"%@,%@错误请求:%@",mutableDic,url,responseDic[@"errmsg"]);
NSMutableDictionary * dic = [NSMutableDictionary dictionary];
[dic setValue:JFERRORMSG forKey:@"errmsg"];
[dic setValue:[NSNumber numberWithInt:-1] forKey:@"errcode"];
compelete(NO, dic);
}else{
compelete(NO, responseDic);
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// if (compelete) {
NSMutableDictionary * dic = [NSMutableDictionary dictionary];
[dic setValue:JFERRORMSG forKey:@"errmsg"];
[dic setValue:[NSNumber numberWithInt:-1] forKey:@"errcode"];
compelete(NO, dic);
// }
}];
}