Mac开发-日志保存过滤以及上传


对于mac开发者来说,进行日志收集和上传尤为重要,没有日志就无法进行分析问题。通常来说我们会将日志文件.log存在 ~/library/logs/YourAppName的目录下,大部分app都是这样放置的,然后缓存文件存在 ~/library/Caches/YourAppName目录下。

简单的日志存储

使用stdout重定向来将控制台的日志写入日志文件.log中,从而达到日志存储的目的

freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);

通常来说,日志文件名称需要加上日志以及app名等相关信息,以便于筛选。

- (void)redirectNSlogToDocumentFolder{
   
    //这里判断是否连接xcode,如果连接xcode我们更希望是输出到控制台,这样我们能更能直接进行调试
#if DEBUG
#else
    if (isatty(STDOUT_FILENO)){
   
        NSLog(@"Appdelegate redirectNSlogToDocumentFolder failed : connected xcode mode");
        return;
    }
#endif
    NSString *genseePath = [self mylogPath];
    NSFileManager* manager = [NSFileManager defaultManager];
    [manager createDirectoryAtPath:genseePath withIntermediateDirectories:NO attributes:NULL error:NULL];
    NSString* bundleName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
    NSString* bundleID = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    //这里使用的日期格式为 yyyyMMdd-HHmmss
    [dateFormatter setDateFormat:@"yyyyMMdd-HHmmss"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@_%@_%@.log",bundleName,bundleID,dateString];
    _logPath = fileName;
    NSString *logFilePath = [genseePath stringByAppendingPathComponent:fileName];
    NSLog(@"[Appdelegate] redirectNSlog
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值