NSLogger的使用


用途

用来监控app的日志,从此摆脱xcode的控制台,mac版界面如下:
mac版qq客户端的日志

使用步骤:

安装 NSLogger Viewer MAC端

当我安装NSLogger Viewer之后发现它监听到了macQQ的日志,看来macQQ也有使用NSLogger

在项目中加入NSLogger框架

在podFile中添加pod 'NSLogger'之后执行 pod install ;或者手动拖入到项目

在项目中使用

在必要的地方添加#import "NSLogger.h"头文件即可

我的一些处理(供参考)

  1. 添加一个宏,自由切换xcode控制台和NSLogger

    // 使用NSLogger打印:取消注释下一行;使用xcode控制台:注释下一行
    //#define NSLOGGER_CLIENT
  2. 导入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
然后查看LogMessageF的实现

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);
// }
}];
}
坚持原创技术分享,您的支持将鼓励我继续创作!