iOS真机上查看NSLog日志

前言

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,在其中获取日志。

创建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
4. 添加跳转日志口子即可查看日志了

样式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值