iOS本地埋点记录

2024-07-09  本文已影响0人  香烟不灭
 dispatch_async(dispatch_get_main_queue(), ^{
            NSString *log = [NSString stringWithFormat:@"发送==%@==%@",[AppUtils timeWithStiring:[NSDate date] Withformat:@"yyyy-MM-dd HH:mm:ss"],urlStr];
            [self logstrSet:log];
            [[AppUtils getCurrentVC].view makeToast:log duration:1.5 position:CSToastPositionBottom];
        });



-(void)logstrSet:(NSString*)logText{
    
    // 获取 Documents 目录路径
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

    // 创建日志文件路径
    NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:@"myapp.log"];

    // 打开文件,并追加写入日志信息
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:logFilePath];
    if (fileHandle == nil) {
        [[NSFileManager defaultManager] createFileAtPath:logFilePath contents:nil attributes:nil];
        fileHandle = [NSFileHandle fileHandleForWritingAtPath:logFilePath];
    }

    if (fileHandle != nil) {
        // 将日志信息写入文件
        NSString *logMessage = [NSString stringWithFormat:@"\n%@========%@", [NSDate date], logText];
        [fileHandle seekToEndOfFile];
        [fileHandle writeData:[logMessage dataUsingEncoding:NSUTF8StringEncoding]];
        [fileHandle closeFile];
    }
 
}

自己写个埋点,记录下。

上一篇下一篇

猜你喜欢

热点阅读