iOS--日志重定向

iOS–日志重定向

一、使用场景:

  • 有时候我没在调试的时候不方便拿到一些信息。如在真机环境下,我们想测试离线推送,而此时我们的APP并没有在调试环境下,所以拿不到相应的log(日志),这时候就可以使用日志重定向。

二、使用步骤

  • 1、appdelegate里面的启动-调用下面方法
-(BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    /** 重定向log日志 */
    //在 info.plist 中打开 Application supports iTunes file sharing
    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPhone Simulator"]) {
        [self redirectNSlogToDocumentFolder];
    }
}
  • 2、重定向方法
//重定向到本地融云log日志
-(void)redirectNSlogToDocumentFolder {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];

    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"MMddHHmmss"];
    NSString *formattedDate = [dateformatter stringFromDate:currentDate];

    NSString *fileName = [NSString stringWithFormat:@"rc%@.log", formattedDate];
    NSString *logFilePath =
    [documentDirectory stringByAppendingPathComponent:fileName];

    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+",
            stdout);
    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+",
            stderr);
}
  • 3、查看log日志–在 Supporting Files 中的 info.plist 中打开 Application supports iTunes file sharin
    这里写图片描述
  • 5、滚动到最下面,点击“存储到…”按钮将 Log 存储到桌面,双击打开就能看到 Log 信息了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值