前言sentry介绍sentry是国外的异常监控平台,支持市面上大部分语言和框架。就react-native而言:它可以自动捕获native错误和js错误。上报信息包括机型、系统版本号、APP版本号、consolex.log信息、错误堆栈信息、异常源码位置、commit记录等。功能包括错误留言板、监控预警、性能监控等。优势:免费举报,信息丰富缺点:需要科学上网,无中文版接入步骤创建项目登录注册,登录哨兵账号。单击创建组织,输入组织名称并确认。我创建了一个名为测试的组织。选择react-native创建项目,可以修改项目名称和组织,这里我就不改了,点击createproject。配置sentry安装npminstall--save@sentry/react-native#oryarnadd@sentry/react-native修改native配置npx@sentry/wizard-ireactNative-piosandroid#oryarnsentry-wizard-ireactNative-piosandroid#上面命令完成后,记得更新ios依赖npxpod-install这时候命令行会打开浏览器(如果没有打开,手动点击命令行出现的链接),然后你选择一个项目,这里选择test/react-nativeproject。代表测试组织下的react-native项目。选择完成后,会自动配置四个文件(写这篇文章时哨兵版本是4.2.4,以后新版本可能会有所不同)。修改内容iOS和Android都有一个sentry.properties文件。sentry.properties里面的内容是一样的:defaults.url=https://sentry.io/defaults.org=test-5yndefaults.project=react-nativeauth.token=4824f7eb99fc06e55d01ad794dd9743fdxxxxxxxxxxxxurl:官网地址org:组织名称project:projectnametoken:authenticationtoken注意,不要尝试在sentry.properties文件中添加注释,否则上传sourcemap时会报错,上传时会使用url参数。androidandroid也修改了android/app/build.gradle文件。iOSios修改了ios/rn_demo.xcodeproj/project.pbxproj文件。添加了构建阶段任务。任务内容大概是在打包的时候上传一些内容到哨兵平台,方便异常定位。估计是定位原生异常。然后我改了react-native原来的打包任务:这里格式化脚本内容:originalscriptset-eWITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"REACT_NATIVE_XCODE="../node_modules/react-native/scripts/react-native-xcode.sh"/bin/sh-c"$WITH_ENVIRONMENT$REACT_NATIVE_XCODE"修改脚本导出SENTRY_PROPERTIES=sentry.propertiesexportEXTRA_PACKAGER_ARGS="--sourcemap-output$DERIVED_FILE_DIR/main.jsbundle.map"set-eWITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"../node_modules/@sentry/cli/bin/sentry-clireact-nativexcodeREACT_NATIVE_XCODE="../node_modules/react-native/scripts/react-native-xcode.sh"/bin/sh-c"$WITH_ENVIRONMENT$REACT_NATIVE_XCODE"大体改动就是增加一个sentrycli命令,然后上传sourcemap方便定位js不寻常的位置。InitializeSDKimport*asSentryfrom'@sentry/react-native';Sentry.init({dsn:'https://04c9544cd0104f97ad0a9c2599f9cd49@o1379748.ingest.sentry.io/6692866',//将此处替换为项目的dsntracesSampleRate:1.0,//上传异常的比例,1.0表示100%上传});包装你的App修改App.tsx:exportdefaultSentry.wrap(App);验证js错误thrownewError("MyfirstSentryerror!");后台查看:nativeerrorSentry.nativeCrash();后台查看:踩坑记录error:Nosuchfileordirectory(oserror2)新版本rn,版本>0.69会出现如下问题。解决方案需要手动修改打包脚本,将以下代码复制到如图所示的地方:../node_modules/react-native/scripts/xcode/with-environment.sh"REACT_NATIVE_XCODE="../node_modules/react-native/scripts/react-native-xcode.sh"SENTRY_CLI_PATH="../node_modules/@sentry/cli/bin/sentry-cli"/bin/sh-c"$WITH_ENVIRONMENT\"$SENTRY_CLI_PATHreact-nativexcode$REACT_NATIVE_XCODE\""env:node:Nosuchfileordirectory如果使用nvm管理node,则运行时会出现以下问题:解决方法创建节点软链接:ln-s$(whichnode)/usr/local/bin/node后记对于被墙的问题,可以考虑私有化部署。详情请查看:https://github。com/getsentry/….参考:哨兵官方文档
