无论如何要将 javascript 控制台日志推送到哨兵

Is there anyway to push javascript console logs to sentry

我在我的应用程序的 man 地方使用 console.error() console.warn() 来跟踪失败的代码。反正有没有自动将这些记录到哨兵。 https://sentry.io

这是一个反应应用程序,他们建议的 componentDidCatch() 方法似乎只捕获异常。

这样做的一种方法是用您自己的自定义实现覆盖 console.errorconsole.warn,因此每当代码的任何部分调用 console.errorconsole.warn 调用将被您的自定义函数拦截,您可以在其中对其执行所需的操作。

以下示例显示了 console.error 方法的自定义实现。

//Store the original reference of console.error
var orgError = console.error;
//Overwirte the default function
console.error = function(error) {
  //All error will be intercepted here
  alert("Intercepted -> " + error);
  //Invoke the original console.error to show the message in console
  return orgError(error);
}
try {
  //Following line will throw error
  nonExistentFunction();
} catch (error) {
  console.error(error);
}

这个问题的选择答案通常是反模式。您永远不想在浏览器中覆盖标准 API,因为应用程序中的其他代码实现可能期望行为是特定的方式,这可能会导致错误。此外,未来为您的代码库做出贡献的开发人员可能不知道您正在劫持标准并最终错误地使用它。

看看我专门为解决这些类型的问题而创建的开源项目:https://adzejs.com

使用 Adze,您可以创建日志侦听器,只要触发目标级别的特定日志,就会触发该侦听器。然后,您可以从侦听器向 Sentry 触发事件。