美文网首页iOS备忘录
ios端实现sentry日志捕获

ios端实现sentry日志捕获

作者: wangyu2488 | 来源:发表于2018-11-01 14:44 被阅读0次

    一.官网:https://sentry.io/ 注册个账号即可
    文档:https://docs.sentry.io/clients/cocoa/

    1.效果


    image.png

    2.sentry主要作用:用于捕获奔溃和自定义异常,便于编译程序员及早发现和解决程序bug

    二.实现
    1.https://sentry.io/ 创建对应类型的应用程序,获取DSN

    image.png
    之后获取Public DSN image.png
    2.工程Podifile引入sentry库
    platform :ios, '8.0'
    
    target '317hu' do
        pod 'Sentry', :git => 'https://github.com/getsentry/sentry-cocoa.git', :tag => '4.1.0'
    end
    

    3.初始化相关代码

    #import <Sentry/Sentry.h>
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        //启动sentry异常捕获
        [self startSentry];
    ...
        return YES;
    }
    
    //启动sentry日志捕获
    - (void)startSentry{
        //1.设置dsn
        NSError *error = nil;
        SentryClient *client = [[SentryClient alloc] initWithDsn:@"https://ab2e9cbc81934286a097d7bd6d101ef3@sentry.io/1313479" didFailWithError:&error];
        SentryClient.sharedClient = client;
        [SentryClient.sharedClient startCrashHandlerWithError:&error];
        if (nil != error) {
            NSLog(@"%@", error);
        }
        
        //2.开启面包屑高级功能
    //    SentryClient.sharedClient.maxBreadcrumbs = 100;
        [SentryClient.sharedClient enableAutomaticBreadcrumbTracking];
    }
    

    3.捕获自定义错误,在网络回包处处理

    + (void)sendSentryEventWithReqParams:(NSDictionary*)reqParams andResParams:(NSDictionary *)resParams url:(NSString *)url reuestMode:(NSString*)mode{
        BOOL successFlag = [resParams[@"success"] boolValue];
        NSString *errMsg = resParams[@"errMsg"];
        //有错误内容时才捕获
        if (!successFlag && errMsg.length > 0) {
            NSMutableDictionary *dic = reqParams.mutableCopy;
            [dic setObject:mode forKey:@"requestMode"];
            [dic setObject:url forKey:@"requestUrl"];
            
            SentryEvent *event = [[SentryEvent alloc] initWithLevel:kSentrySeverityDebug];
            event.message = errMsg;
            event.environment = kEnv;//dev sit uat pro 四个环境
            event.extra = dic; //请求参数
            [SentryClient.sharedClient sendEvent:event withCompletionHandler:^(NSError * _Nullable error) {
                if (nil != error) {
                    NSLog(@"%@", error);
                }
            }];
        }
    }
    
    + (void)testSentry{
    
            [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                [QQRequest sendSentryEventWithReqParams:param andResParams:responseObject url:url reuestMode:@"post"];
    
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
               
            }];
    }
    

    对应效果
    3.1面包屑功能,最近的浏览记录

    image.png
    3.2接口入参相关 image.png
    4.默认sentry会捕获奔溃,需要上传dSYMs符号文件才可以解析奔溃日志。不然就像下面日志一样,看不懂
    image.png
    二.sentry官网实现上传符号文件dSYMs (用fastlane实现) Uploading Debug Symbols
    1.第一步 安装fastlane
    第二步 安装sentry-cli 参考https://github.com/getsentry/sentry-cli
    brew install getsentry/tools/sentry-cli

    curl -sL https://sentry.io/get-cli/ | bash
    2.项目首次使用的话,需要
    首次用fastlane 要执行 cd 到项目根目录,在执行如下命令,
    fastlane add_plugin sentry 【成功后就会有如下文件】 image.png
    3.编写如下脚本,将日志 默认上传到官网
      desc "上传到sentry"
      lane :upload_symbols do
      #download_dsyms
      gym(
          scheme: "317hu",
          workspace: "317hu.xcworkspace",
          include_bitcode: false
          )
      sentry_upload_dsym(
        #api_host: 'https://sentry.317hu.com/fe-master/',
        auth_token: '4ec35b3d884c4f6d94b51ab29adb07603862c036eada4ad09817009ae4b148d5',
        org_slug: 'bozhong-xu',
        project_slug: '317hu_ios',
      )
    
      end
    

    sentry_upload_dsym对应参数位置如下


    image.png
    image.png

    4.终端执行

    wangyuMBP:~ mac$ cd /Users/mac/Desktop/3.8.9官网 
    wangyuMBP:3.8.9官网 mac$ fastlane upload_symbols
    
    成功后如下 image.png
    image.png

    之后就可以查看奔溃内容了


    image.png

    三.其他修改


    image.png

    如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

    相关文章

      网友评论

        本文标题:ios端实现sentry日志捕获

        本文链接:https://www.haomeiwen.com/subject/jmkxxqtx.html