Manifest檔案中
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
導入gradle插件
//sentry 崩潰收集
implementation 'io.sentry:sentry-android:1.7.16'
//标準化日志
implementation 'org.slf4j:slf4j-nop:1.7.25'
Sentry工具類
/**
* author: Tommy
* time : 2019/5/16
* desc :
*/
public class SentryUtils {
//初始化sentry
/**
* Use the Sentry DSN (client key) from the Project Settings page on Sentry
* String sentryDsn = "https://publicKey:[email protected]:port/1?options";
* String sentryDsn = "http://8c4a32c102b04f929f21356d7188c7c5:[email protected]:9000/2";
*/
public static void init(Context context, String sentryDsn) {
Sentry.init(sentryDsn, new AndroidSentryClientFactory(context));
}
//主動發送Throwable消息
public static void sendSentryExcepiton(Throwable throwable) {
Sentry.capture(throwable);
}
//主動發送Event消息
public static void sendSentryExcepiton(Event throwable) {
Sentry.capture(throwable);
}
//主動發送EventBuilder消息
public static void sendSentryExcepiton(EventBuilder throwable) {
Sentry.capture(throwable);
}
public static void sendSentryExcepiton(String logger, Throwable throwable) {
SentryUtils.sendSentryExcepiton(new EventBuilder().withMessage("try catch msg").withLevel(Event.Level.WARNING).withLogger(logger).withSentryInterface(new ExceptionInterface(throwable)));
}
}
內建
String sentryDsn = "http://8c4a32c102b04f929f21356d7188c7c5:[email protected]:9000/2";
Context ctx = this.getApplicationContext();
SentryUtils.init(ctx, sentryDsn);
附上我測試項目的下載下傳位址:https://download.csdn.net/download/u010381752/11185354
其實,很簡單,就這麼點東西,But……
關于 Dsn
Dsn 是什麼鬼?它是送出端與伺服器的一個認證的KEY
具體的生成規則如下:
http://publicKey:[email protected]:port/1?options
即:http 或者 https + :// + publicKey : secretKey @ 伺服器位址: 端口 / Project ID
這麼說肯定還有很多老鐵不清楚,下面進行詳細滴圖文說明:
http://host:9000 登入你的 sentry 服務端【沒有的出門左轉,找到我的 Linux 之 sentry 伺服器搭建】
第一步:建立一個項目
第二步:檢視你的 Client Keys(DSN)
好,到這步看到三個東西:Public Key、Secret Key、Project ID
第三步:合并成你的 DSN
http://Public Key:Secret [email protected]:9000/Project ID
即:http://8c4a32c102b04f929f21356d7188c7c5:[email protected]:9000/2
OK,大功告成~!
需要說明一下的是,如果部署到線上,基本都會用到https的,是以,如果不是本地測試用的話,http 請改成 https了,這個根據你的實際情況而定了。
第四步:測試一波
運作app,進入頁面,點選讓app跑到 throw new NullPointerException()
然後app崩潰,接着到背景看看,發現收到錯誤資訊,如下圖:
PS:最後提醒一下大家,如果你是做本地測試,請一定確定 你手機的網段跟Sentry伺服器是一個網段,否則是通路不到的,切記切記【雲服務系統老鐵自行跳過】