Sentry Getting Started - iOS

官方介入指南:https://docs.sentry.io/clients/cocoa/

一、接入Sentry

Cocoa

This is the documentation for our official clients for Cocoa (Swift and Objective-C). Starting with version 3.0.0 we’ve switched our internal code from Swift to Objective-C to maximize compatibility. Also we trimmed the public API of our sdk to a minimum. Some of the lesser used features that were present before are gone now, check out Migration Guide or Advanced Usage for details.

Getting Started

Getting started with Sentry is a three step process:

Sign up for an account

Install your SDK

Configure it

Installation

The SDK can be installed using CocoaPods or Carthage. This is the recommended client for both Swift and Objective-C.

We recommend installing Sentry with CocoaPods.

To integrate Sentry into your Xcode project using CocoaPods, specify it in your Podfile:

source'https://github.com/CocoaPods/Specs.git'platform:ios,'8.0'use_frameworks!target'YourApp'dopod'Sentry',:git=>'https://github.com/getsentry/sentry-cocoa.git',:tag=>'4.3.1'end

Afterwards run pod install. In case you encounter problems with dependencies and you are on a newer CocoaPods you might have to run pod repo update first.

To integrate Sentry into your Xcode project using Carthage, specify it in your Cartfile:

github"getsentry/sentry-cocoa""4.3.1"

Run carthage update to download the framework and drag the built Sentry.framework into your Xcode project.

We also provide a pre-built version for every release which can be downloaded at releases on GitHub.

Configuration

To use the client, change your AppDelegate’s application method to instantiate the Sentry client:

Showing configuration for 

your-org / your-project 

importSentryfuncapplication(_application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:[UIApplication.LaunchOptionsKey:Any]?)->Bool{// Create a Sentry client and start crash handlerdo{Client.shared=tryClient(dsn:"https://<key>@sentry.io/<project>")tryClient.shared?.startCrashHandler()}catchleterror{print("\(error)")}returntrue}

If you prefer to use Objective-C you can do so like this:

Showing configuration for 

your-org / your-project 

@importSentry;NSError*error=nil;SentryClient*client=[[SentryClientalloc]initWithDsn:@"https://<key>@sentry.io/<project>"didFailWithError:&error];SentryClient.sharedClient=client;[SentryClient.sharedClientstartCrashHandlerWithError:&error];if(nil!=error){NSLog(@"%@",error);}

Debug Symbols

Before you can start capturing crashes you will need to tell Sentry about the debug information by uploading dSYM files. Depending on your setup this can be done in different ways:

With Bitcode

Without Bitcode

Testing a Crash

If you would like to test the crash reporting you will need to cause a crash. While the seemingly obvious method would be to make it crash on launch, this will not give the Sentry client a chance to actually submit the crash report. Instead, we recommend triggering a crash from a button tap.

You can use the following methods to cause a crash:

Swift:

Client.shared?.crash()

Objective-C:

[SentryClient.sharedClientcrash];

Note that if you crash with a debugger attached nothing will happen.

Crashes are only submitted upon re-launching the application. To see the crash in Sentry, close the app and launch it again from the springboard.

二、上传dSYM

Uploading Debug Symbols

A dSYM upload is required for Sentry to symoblicate your crash logs for viewing. The symoblication process unscrambles Apple’s crash logs to reveal the function, variables, file names, and line numbers of the crash. The dSYM file can be uploaded through the sentry-cli tool or through a Fastlane action.

With Bitcode

If Bitcode is enabled in your project, you will have to upload the dSYM to Sentry after it has finished processing in the iTunesConnect. We also recommend using the latest Xcode version for submitting your build. The dSYM can be downloaded in three ways.

Use Fastlane

Use the Fastlane’s action, download_dsyms, to download the dSYMs from iTunesConnect and upload to Sentry. The dSYM won’t be generated until after the app is done processing on iTunesConnect so this should be run in its own lane.

lane:upload_symbolsdodownload_dsymsupload_symbols_to_sentry(auth_token: '...',org_slug: '...',project_slug: '...',)end

If you have a legacy API key instead you can supply it with api_key instead of auth_token.

On Prem

By default fastlane will connect to sentry.io. For on-prem you need to provide the api_host parameter to instruct the tool to connect to your server:

api_host: 'https://mysentry.invalid/'

Use sentry-cli

There are two ways to download the dSYM from iTunesConnect. After you do one of the two following ways, you can upload the dSYM using sentry-cli.

Open Xcode Organizer, go to your app, and click “Download dSYMs…”

Login to iTunes Connect, go to your app, go to “Activity, click the build number to go into the detail page, and click “Download dSYM”

Afterwards manually upload the symbols with sentry-cli:

sentry-cli--auth-tokenYOUR_AUTH_TOKEN upload-dif--orgYOUR_ORG_SLUG--projectYOUR_PROJECT_SLUG PATH_TO_DSYMS

On Prem

By default sentry-cli will connect to sentry.io. For on-prem you need to export the SENTRY_URL environment variable to instruct the tool to connect to your server:

export SENTRY_URL=https://mysentry.invalid/

Without Bitcode

When not using bitcode you can directly upload the symbols to Sentry as part of a build.

Use Fastlane

If you are already using Fastlane you can use it in this situation as well. If you are not, you might prefer the sentry-cli integerated into Xcode.

lane:builddogymupload_symbols_to_sentry(auth_token: '...',org_slug: '...',project_slug: '...',)end

If you have a legacy API key instead you can supply it with api_key instead of auth_token.

On Prem

By default fastlane will connect to sentry.io. For on-prem you need to provide the api_host parameter to instruct the tool to connect to your server:

api_host: 'https://mysentry.invalid/'

Upload Symbols with sentry-cli

Your project’s dSYM can be upload during the build phase as a “Run Script”. For this you need to st the DEBUG_INFORMATION_FORMAT to be DWARF with dSYM File. By default, an Xcode project will only have DEBUG_INFORMATION_FORMAT set to DWARF with dSYM File in Release so make sure everything is set in your build settings properly.

You need to have an Auth Token for this to work. You can create an Auth Token here.

You will need to copy the below into a new Run Script and set your AUTH_TOKENORG_SLUG, and PROJECT_SLUG

Download and install sentry-cli — The best place to put this is in the /usr/local/bin/ directory

Showing configuration for 

your-org / your-project 

if which sentry-cli>/dev/null;thenexport SENTRY_ORG=your-orgexport SENTRY_PROJECT=your-projectexport SENTRY_AUTH_TOKEN=YOUR_AUTH_TOKENERROR=$(sentry-cli upload-dif"$DWARF_DSYM_FOLDER_PATH"2>&1>/dev/null)if[!$?-eq0];thenecho"warning: sentry-cli - $ERROR"fi

elseecho"warning: sentry-cli not installed, download from https://github.com/getsentry/sentry-cli/releases"fi

On Prem

By default sentry-cli will connect to sentry.io. For on-prem you need to export the SENTRY_URL environment variable to instruct the tool to connect to your server:

export SENTRY_URL=https://mysentry.invalid/

Manually with sentry-cli

Your dSYM file can be upload manually by you (or some automated process) with the sentry-cli tool. You will need to know the following information:

API Key

Organization slug

Project slug

Path to the build’s dSYM

Download and install sentry-cli — The best place to put this is in the /usr/local/bin/ directory.

Then run this – note that --auth-token needs to go before upload-dif:

sentry-cli--auth-tokenYOUR_AUTH_TOKEN upload-dif--orgYOUR_ORG_SLUG--projectYOUR_PROJECT_SLUG PATH_TO_DSYMS

On Prem

By default, sentry-cli will connect to sentry.io. For on-prem you need to export the SENTRY_URL environment variable to instruct the tool to connect to your server:

export SENTRY_URL=https://mysentry.invalid/

ON THIS PAGE

With Bitcode

Use Fastlane

Use sentry-cli

Without Bitcode

Use Fastlane

Upload Symbols with sentry-cli

Manually with sentry-cli

Found an error? Help us fix it

三、使用Swift实例

1、Podfile增加

pod 'Sentry', :git => 'https://github.com/getsentry/sentry-cocoa.git', :tag => '4.3.1'

2、初始化CrashHandler及日志上报代码

//配置sentry日志监控

 // Create a Sentry client and start crash handler 水滴 xxxxx代表示例测试地址,使用时替换成自己的地址

 do {

 Client.shared = try Client(dsn: "https://xxxxxxxxx@sentry.xxx")

 try Client.shared?.startCrashHandler()

} catch let error {

 print("\(error)")

        }

 //sentry测试信息 crash event

 Client.shared?.crash()

 let event = Event(level: .debug)

event.message = "Test Message"

 Client.shared?.send(event: event) { (error) in

 // Optional callback after event has been sent

        }

3、dSYM配置

Upload Symbols with sentry-cli

Your project’s dSYM can be upload during the build phase as a “Run Script”. For this you need to st the DEBUG_INFORMATION_FORMAT to be DWARF with dSYM File. By default, an Xcode project will only have DEBUG_INFORMATION_FORMAT set to DWARF with dSYM File in Release so make sure everything is set in your build settings properly.

You need to have an Auth Token for this to work. You can create an Auth Token here.

You will need to copy the below into a new Run Script and set your AUTH_TOKENORG_SLUG, and PROJECT_SLUG

Download and install sentry-cli — The best place to put this is in the /usr/local/bin/ directory

Showing configuration for 

your-org / your-project 

if which sentry-cli>/dev/null;thenexport SENTRY_ORG=your-orgexport SENTRY_PROJECT=your-projectexport SENTRY_AUTH_TOKEN=YOUR_AUTH_TOKEN

export SENTRY_URL= https://sentry.sample.cnERROR=$(sentry-cli upload-dif"$DWARF_DSYM_FOLDER_PATH"2>&1>/dev/null)if[!$?-eq0];thenecho"warning: sentry-cli - $ERROR"fi

elseecho"warning: sentry-cli not installed, download from https://github.com/getsentry/sentry-cli/releases"fi

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 224,383评论 6 522
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 96,028评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 171,486评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,786评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,793评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,297评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,685评论 3 428
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,655评论 0 279
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,192评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,223评论 3 345
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,343评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,966评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,656评论 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,130评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,257评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,869评论 3 381
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,407评论 2 365

推荐阅读更多精彩内容