iOS開發(fā)日志通過ftp上傳的方法

在開發(fā)過程中,需要收集奔潰日志,那怎么做到奔潰日志上傳呢,通常集成第三方工具,什么友盟、蒲公英、騰訊之類的統(tǒng)計軟件,但是有的公司有要求,奔潰日志需要存放在服務器上面,本文就介紹下通過FTP搜集日志的方法:
1、實現(xiàn)C語言通過的的方法uncaughtExceptionHandler捕獲奔潰日志, 將崩潰信息持久化在本地,下次程序啟動時,將崩潰信息作為日志發(fā)送給開發(fā)者

CatchCrash.h
#import <UIKit/UIKit.h>
@interface CatchCrash : NSObject

/**
 *  異常處理函數(shù)
 *
 *  @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\"系統(tǒng)名稱\":\"%@\",\n",[DeviceInfo getDeviceInfo].systemName];
    [exceptionJSON appendFormat:@"\t\"設備名稱\":\"%@\",\n",[DeviceInfo getDeviceInfo].deviceName];
    [exceptionJSON appendFormat:@"\t\"系統(tǒng)版本\":\"%@\",\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\"異常堆棧\":\n\t\""];
    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];
}

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

當然也可以調用后臺提供的接口,當下次開啟APP將奔潰日志上傳,此外還可以通過發(fā)郵件的方式將日志發(fā)送到指定郵箱。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。