Android 之 Sentry 集成

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:secretKey@host:port/1?options";
     * String sentryDsn = "http://8c4a32c102b04f929f21356d7188c7c5:40945f7672654e949f572c98bf942fb2@10.10.19.203: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:40945f7672654e949f572c98bf942fb2@10.10.19.203:9000/2";      
Context ctx = this.getApplicationContext();
SentryUtils.init(ctx, sentryDsn);

附上我测试项目的下载地址:https://download.csdn.net/download/u010381752/11185354

其实,很简单,就这么点东西,But……

关于 Dsn 

Dsn 是什么鬼?它是提交端与服务器的一个认证的KEY

具体的生成规则如下:

http://publicKey:secretKey@host: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 Key@host:9000/Project ID

即:http://8c4a32c102b04f929f21356d7188c7c5:40945f7672654e949f572c98bf942fb2@10.10.19.203:9000/2

OK,大功告成~!

需要说明一下的是,如果部署到线上,基本都会用到https的,所以,如果不是本地测试用的话,http 请改成 https了,这个根据你的实际情况而定了。

第四步:测试一波

运行app,进入页面,点击让app跑到  throw new NullPointerException()

然后app崩溃,接着到后台看看,发现收到错误信息,如下图:

PS:最后提醒一下大家,如果你是做本地测试,请一定确保 你手机的网段跟Sentry服务器是一个网段,否则是访问不到的,切记切记【云服务系统老铁自行跳过】

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 可以很方便地集成 Sentry,以下是集成步骤: 1. 在 `pom.xml` 文件中添加 Sentry 依赖: ```xml <dependency> <groupId>io.sentry</groupId> <artifactId>sentry-spring-boot-starter</artifactId> <version>3.1.0</version> </dependency> ``` 2. 在 `application.properties` 或 `application.yml` 文件中配置 Sentry: ```properties sentry.dsn=YOUR_DSN sentry.release=YOUR_RELEASE_VERSION sentry.environment=YOUR_ENVIRONMENT ``` 其中,`YOUR_DSN` 是你的 Sentry DSN(Data Source Name),`YOUR_RELEASE_VERSION` 是你的应用版本号,`YOUR_ENVIRONMENT` 是你的应用环境(如 `production`、`development` 等)。 3. 创建一个全局异常处理器类,用于捕获异常并发送到 Sentry: ```java import io.sentry.Sentry; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.servlet.ModelAndView; @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ModelAndView handleException(Exception e) { // 发送异常信息到 Sentry Sentry.captureException(e); // 返回错误页面或其他处理方式 ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("error"); modelAndView.addObject("errorMessage", "An error occurred"); return modelAndView; } } ``` 以上步骤完成后,当应用发生异常时,Sentry 将会自动捕获并发送相应的异常信息。你可以登录 Sentry 平台查看应用的异常报告和性能指标。 希望以上信息对你有帮助!如果你还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

s清风s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值