android - 从已部署的 Cordova 应用访问 console.log/error

标签 android cordova ionic-framework hybrid-mobile-app

我知道您可以在浏览器上和通过 adb 查看 cordova 应用程序的控制台。

但我想要的是用户能够向我发送错误报告。在该错误报告中,我希望包含出现的 console.error 的内容。

这有可能吗?

最佳答案

至少您需要:

  • 添加一个 window.onerror 处理程序。它将由您应用中任何未处理的异常触发。 (不过,这对捕获的错误没有任何作用。)参见 https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror了解更多信息。

  • 添加一些发送错误报告的方法。您可以使用 HTML 和少量 JavaScript(使用 XHR)完成此操作。确保您使用的后端是安全的。

更全面的选项将为您提供更多有用的信息:

  • 创建一个日志记录方法来处理您应用的所有日志记录需求。它应该能够处理日志级别,如infowarnerror等。日志记录方法应该保存最近的日志,以便他们可以包含在任何错误报告中。

    NOTE: You probably don't want to save everything, since that could be memory intensive. You may only want to track the last 100 or so logs entries.

  • 如果发生错误,让您的记录器要求用户使用报告方法(如前一节所述)发送错误报告。因为您已经记录了额外的数据,所以您可以更全面地记录应用中到目前为止发生的事情。

不过,最终,我会四处寻找一个错误处理和报告库,这样您就不需要重新发明轮子了。

NOTE: In all cases you should be careful what you log and where and how you send that data. You shouldn't log anything that would compromise privacy or security (e.g., passwords), and you should send the logs over secured channels and store the logs on your backend securely. You should mention how you store and handle this information (including what information is collected) in your privacy policy as well.

关于android - 从已部署的 Cordova 应用访问 console.log/error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42598599/

相关文章:

java - 忽略 String.contain 上的符号

javascript - PhoneGap确认回调函数

javascript - PhoneGap登录页面,如何保存用户信息

javascript - 防止 jQuery Mobile 中的 ajax 缓存

android - Ionic Music Controls 不工作 | Ionic 3 + Ionic Native Media + Ionic Native 音乐控制

Java/Android 数学测试 : Got Curious About Int and Float Math/Conversion/Casting

android - 从 Android 代码设置的 SECRET_CODE

android - 在 Ionic 框架中构建启动画面动画?

angularjs - Angular/Ionic 链接更新 URL,但未调用 Controller 和模板

android - 动画列表在 Android 5.0 (Lollipop) 中不起作用