iOS开采日志通过ftp上传的主意,iOS崩溃时征集崩

1.在App上线时大家平素不主意log手提式无线电话机的奔溃日志,未来网络有无尽第三方收罗奔溃日志的方法,常用的比方说Crashlytics2.但大家也亟需驾驭怎么采用系统框架收罗crash日志

在付出进度中,必要搜聚奔溃日志,那怎么做到奔溃日志上传呢,经常集成第三方工具,什么友盟、小金英、Tencent等等的总结软件,可是某个公司有要求,奔溃日志供给寄存在服务器上面,本文就介绍下通过FTP搜聚日志的办法:
1、完结C语言通过的的章程uncaughtExceptionHandler破获奔溃日志, 将夭折消息长久化在地头,后一次前后相继运维时,将崩溃音讯作为日志发送给开垦者

一、在App崩溃时征集崩溃消息

- application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // 1.添加Crash崩溃函数监听 NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler); return YES;}// 当程序Crash时掉用此函数 void UncaughtExceptionHandler(NSException *exception) { // 1.异常类型 NSString *name = [exception name]; // 2.非常重要,就是崩溃的原因 NSString *reason = [exception reason]; // 3.得到当前调用栈信息 NSArray *logArray = [exception callStackSymbols]; // 4.崩溃日志 NSString *crashLogInfo = [NSString stringWithFormat:@"exception type : %@ n crash reason : %@ n call stack info : %@", name, reason, logArray]; // 5.可以缓存写入到本地,下次程序开启时发送的远端服务器 // 再次我设置崩溃信息发送至邮箱 NSString *urlStr = [NSString stringWithFormat:@"mailto://siming_zhu@163.com?subject=bug报告&body=感谢您的配合!,错误详情:%@", crashLogInfo]; NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL:url];}
CatchCrash.h
#import <UIKit/UIKit.h>
@interface CatchCrash : NSObject

/**
 *  异常处理函数
 *
 *  @param exception 异常对象
 */
void uncaughtExceptionHandler(NSException *exception);
@end

CatchCrash.m
void uncaughtExceptionHandler(NSException *exception){
    NSMutableString *exceptionJSON = [[NSMutableString alloc] init];
    [exceptionJSON appendString:@"{n"];
    [exceptionJSON appendFormat:@"t"异常时间":"%@",n",[CatchCrash getCurrentTime]];
    [exceptionJSON appendFormat:@"t"用户名字":"%@",n",[KKInfoHelper readUserInfo].username];
    [exceptionJSON appendFormat:@"t"设备型号":"%@",n",[DeviceInfo getDeviceInfo].deviceModel];
    [exceptionJSON appendFormat:@"t"系统名称":"%@",n",[DeviceInfo getDeviceInfo].systemName];
    [exceptionJSON appendFormat:@"t"设备名称":"%@",n",[DeviceInfo getDeviceInfo].deviceName];
    [exceptionJSON appendFormat:@"t"系统版本":"%@",n",[DeviceInfo getDeviceInfo].systemVersion];
    [exceptionJSON appendFormat:@"t"应用版本":"%@",n",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
    [exceptionJSON appendFormat:@"t"异常名称":"%@",n",[exception name]];
    [exceptionJSON appendFormat:@"t"异常原因":"%@",n",[exception reason]];
    [exceptionJSON appendString:@"t"异常堆栈":nt""];
    for (NSString* str in [exception callStackSymbols]) {
        [exceptionJSON appendFormat:@"[%@]",str];
    }
    [exceptionJSON appendString:@"",n"];
    [exceptionJSON appendString:@"}"];
    [exceptionJSON writeToFile:[NSString stringWithFormat:@"%@/Documents/CrashError.log",NSHomeDirectory()] atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

愈来愈多分享:

Github:

2、通过GoldRaccoon本条第三方ftp框架将写入本地的文件上传出服务器,

UploadCrashLog.h文件
#import <Foundation/Foundation.h>
@interface UploadCrashLog : NSObject

/**
 *  上传崩溃日志实例
 */
+ (UploadCrashLog*)getInstance;

/**
 *  开始上传崩溃日志
 */
- (void)startUpload;

@end

UploadCrashLog.m文件
#import "UploadCrashLog.h"
#import "Global.h"
#import "AppDelegate.h"
#import "KKValidate.h"
#import "GRRequestsManager.h"

@interface UploadCrashLog ()<GRRequestsManagerDelegate> {
    BOOL _connected;
}
@property (nonatomic, strong) GRRequestsManager *requestsManager;
@property (nonatomic, strong) NSString *filePath;
@end

@implementation UploadCrashLog

/**
 *  上传崩溃日志实例
 */
+ (UploadCrashLog*)getInstance {
    static UploadCrashLog *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });

    return sharedInstance;
}

/**
 *  开始上传崩溃日志
 */
- (void)startUpload {
    //[self performSelectorInBackground:@selector(uploadProcess:) withObject:nil];
    [NSThread detachNewThreadSelector:@selector(uploadProcess:) toTarget:self withObject:nil];
}

-(GRRequestsManager *)requestsManager {
    if (!_requestsManager) {
        NSString* hostName = CrashLogSFTPHostName;
        NSInteger port = CrashLogSFTPPort;
        NSString* userName = CrashLogSFTPUserName;
        NSString* password = CrashLogSFTPPassword;
        _requestsManager=[[GRRequestsManager alloc] initWithHostname:hostName user:userName password:password];
        self.requestsManager.delegate = self;
    }
    return _requestsManager;
}

- (void)uploadProcess:(id)sender {
    NSString* logRemotePath = CrashLogSFTPLogDir;
    NSString* logLocalPath = [NSString stringWithFormat:@"%@/Documents/CrashError.log",NSHomeDirectory()];
    //先判断是否有崩溃日志
    NSFileManager* fileManager = [NSFileManager defaultManager];
    BOOL isDirectory;
    BOOL isPathExist = [fileManager fileExistsAtPath:logLocalPath isDirectory:&isDirectory];
    if (!isPathExist || isDirectory) {
        return;
    }

    logRemotePath = [logRemotePath stringByAppendingPathComponent:[NSString stringWithFormat:@"/Stomatiology%.f.log",[[NSDate date]timeIntervalSince1970]*1000000]];
    [self.requestsManager addRequestForUploadFileAtLocalPath:logLocalPath toRemotePath:logRemotePath];
    [self.requestsManager startProcessingRequests];
}

/**
 *  获取yyyy-MM-dd HH:mm:ss格式的时间
 */
-(NSString*)getCurrentTime {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSZ"];
    return [dateFormatter stringFromDate:[NSDate date]];
}


- (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didCompleteUploadRequest:(id<GRDataExchangeRequestProtocol>)request
{
    NSLog(@"requestsManager:didCompleteUploadRequest:%@",@"上传崩溃信息成功");
    NSFileManager* fileManager = [NSFileManager defaultManager];
    //删除本地崩溃日志文件
    NSString* logLocalPath = [NSString stringWithFormat:@"%@/Documents/CrashError.log",NSHomeDirectory()];
    [fileManager removeItemAtPath:logLocalPath error:nil];
}

- (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didFailWritingFileAtPath:(NSString *)path forRequest:(id<GRDataExchangeRequestProtocol>)request error:(NSError *)error
{
    NSLog(@"requestsManager:didFailWritingFileAtPath:forRequest:error: n %@", error);
}

- (void)requestsManager:(id<GRRequestsManagerProtocol>)requestsManager didFailRequest:(id<GRRequestProtocol>)request withError:(NSError *)error
{
    NSLog(@"requestsManager:didFailRequest:withError: n %@", error);
}
@end

理之当然也足以调用后台提供的接口,当后一次开启应用程式将奔溃日志上传,另外还足以经过发邮件的法子将日志发送到钦赐邮箱。

本文由华夏彩票发布于编程应用,转载请注明出处:iOS开采日志通过ftp上传的主意,iOS崩溃时征集崩

您可能还会对下面的文章感兴趣: