前言
CocoaLumberjack捆绑了一系列的内置日志记录器,通过简单的配置,就可以实现在手机上直接查看nslog日志的功能,对于真机调试有极大的帮助。
配置过程
1. pod导入CocoaLumberjack库
pod 'CocoaLumberjack'
2. 创建NSLog宏,拦截系统NSLog方法
#ifdef DEBUG
#define NSLog(...) DDLogDebug(@"AppName %s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define NSLog(...)
#endif
3. 创建日志查看VC,在其中获取日志。
代码展示
#import "YXLogManagerVC.h"
@interface YXLogManagerVC ()
@property (weak, nonatomic) IBOutlet UITextView *textView;
@end
@implementation YXLogManagerVC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
DDFileLogger *fileLoggerOne = DDLog.allLoggers.lastObject;
// NSLog(@"logsDirectory *** %@",fileLoggerOne.currentLogFileInfo.filePath);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *path = fileLoggerOne.currentLogFileInfo.filePath;
NSString *readStr= [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
//回到主线程
self.textView.text = readStr;
self.textView.layoutManager.allowsNonContiguousLayout = NO;
[self.textView scrollRangeToVisible:NSMakeRange(self.textView.text.length, 1)];
});
});
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 隐藏导航条
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
// MARK: 返回按钮点击
- (IBAction)backButtonClick:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
// MARK: 清空按钮
- (IBAction)cleanButtonClick:(UIButton *)sender {
DDFileLogger *fileLoggerOne = DDLog.allLoggers.lastObject;
//
[fileLoggerOne rollLogFileWithCompletionBlock:^{
/// 回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
self.textView.text = @"清空成功";
});
}];
}
- (IBAction)scrollToBottomClick:(YXXibButton *)sender {
[self.textView scrollRangeToVisible:NSMakeRange(self.textView.text.length, 1)];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end