一.官網(wǎng):https://sentry.io/ 注冊個賬號即可
文檔:https://docs.sentry.io/clients/cocoa/
1.效果
image.png
2.sentry主要作用:用于捕獲奔潰和自定義異常昏滴,便于編譯程序員及早發(fā)現(xiàn)和解決程序bug
二.實現(xiàn)
1.https://sentry.io/ 創(chuàng)建對應類型的應用程序骇吭,獲取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.捕獲自定義錯誤,在網(wǎng)絡回包處處理
+ (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 四個環(huán)境
event.extra = dic; //請求參數(shù)
[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接口入?yún)⑾嚓P
image.png
4.默認sentry會捕獲奔潰痪寻,需要上傳dSYMs符號文件才可以解析奔潰日志螺句。不然就像下面日志一樣,看不懂
image.png
二.sentry官網(wǎng)實現(xiàn)上傳符號文件dSYMs (用fastlane實現(xiàn)) 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 要執(zhí)行 cd 到項目根目錄蛇尚,在執(zhí)行如下命令,
fastlane add_plugin sentry 【成功后就會有如下文件】
image.png
3.編寫如下腳本顾画,將日志 默認上傳到官網(wǎng)
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對應參數(shù)位置如下
image.png
image.png
4.終端執(zhí)行
wangyuMBP:~ mac$ cd /Users/mac/Desktop/3.8.9官網(wǎng)
wangyuMBP:3.8.9官網(wǎng) mac$ fastlane upload_symbols
成功后如下image.png
image.png
之后就可以查看奔潰內容了
image.png
三.其他修改
image.png
如果您發(fā)現(xiàn)本文對你有所幫助取劫,如果您認為其他人也可能受益,請把它分享出去研侣。