是否可以将所有浏览器控制台 messages/errors 通过管道传输到远程计算机?

Is it possible to pipe all browser console messages/errors to a remote machine?

我目前正在尝试调试 Cordova 应用程序的 InAppBrowser(在 Android 上)发生的 javascript 错误。不幸的是,虽然我可以使用 chrome 的远程 debugging/inspection 工具连接到 phone 上的 Web 视图,但控制台输出为空,尽管它不应该是空的。 (无论是否遇到问题,都应该显示杂项 messages/warnings。)

此时,我在尝试获取 console/error 消息时遇到了障碍。似乎没有任何效果。谁能想出一种方法让 webview/javascript 将所有错误消息转发到远程地址? (显然,我永远不会发布具有这种配置的应用程序。)

如果是 JavaScript 错误,您也许可以使用 window.onerror 捕获它。这个想法是捕获错误,然后 post 将其发送到日志服务器。

window.onerror = function (msg, url, lineNo, columnNo, error) {
  alert(msg)
  // Log error to external server.
  // axios.post(url, JSON.stringify({msg, url, lineNo, columnNo, error}))
}

const test = undefined

test.error

我在我的生产环境中这样做。这有助于捕获错误并更快地将问题通知我。

如果您有兴趣将错误和其他信息记录到另一台机器上,您可以使用 Reporting API, where one uses the Report-To server header (potentially along with other headers) to enable reporting of specified errors to a reporting endpoint. Here's list of some of the possible report types (more details on Network Error logging). It's currently supported in Chrome/Edge, and can be enabled through about:config in Firefox