[英]How to report console.error with Sentry?
我有一个应用程序,其中使用console.error
报告了一些关键问题,但没有thrown
因此应用程序可能会继续运行 - 可能处于瘫痪状态。
也有必要报告console.error
问题,但 Sentry (Raven) 库只向服务器发送抛出的异常。
有人知道如何很好地解决这个问题吗?
(理想情况下不需要重写所有console.error
调用,因为一些供应商库可能仍然只将输出写入控制台)
正如用户@kumar303 在他对问题的评论中提到的那样......您可以使用 JS 控制台集成Sentry.Integrations.CaptureConsole
。
有关文档,请参阅https://docs.sentry.io/platforms/javascript/configuration/integrations/plugin/#captureconsole 。
最后,用于设置 Sentry 的 JS 代码如下所示:
import * as Sentry from '@sentry/browser';
import { CaptureConsole } from '@sentry/integrations';
Sentry.init({
dsn: 'https://your-sentry-server-dsn',
integrations: [
new CaptureConsole({
levels: ['error']
})
],
release: '1.0.0',
environment: 'prod',
maxBreadcrumbs: 50
})
如果然后有人调用console.error
一个新事件将发送到哨兵。
这是一个更强大的覆盖解决方案
// creating function declarations for better stacktraces (otherwise they'd be anonymous function expressions)
var oldConsoleError = console.error;
console.error = reportingConsoleError; // defined via function hoisting
function reportingConsoleError() {
var args = Array.prototype.slice.call(arguments);
Sentry.captureException(reduceConsoleArgs(args), { level: 'error' });
return oldConsoleError.apply(console, args);
};
var oldConsoleWarn = console.warn;
console.warn = reportingConsoleWarn; // defined via function hoisting
function reportingConsoleWarn() {
var args = Array.prototype.slice.call(arguments);
Sentry.captureMessage(reduceConsoleArgs(args), { level: 'warning' });
return oldConsoleWarn.apply(console, args);
}
function reduceConsoleArgs(args) {
let errorMsg = args[0];
// Make sure errorMsg is either an error or string.
// It's therefore best to pass in new Error('msg') instead of just 'msg' since
// that'll give you a stack trace leading up to the creation of that new Error
// whereas if you just pass in a plain string 'msg', the stack trace will include
// reportingConsoleError and reportingConsoleCall
if (!(errorMsg instanceof Error)) {
// stringify all args as a new Error (which creates a stack trace)
errorMsg = new Error(
args.reduce(function(accumulator, currentValue) {
return accumulator.toString() + ' ' + currentValue.toString();
}, '')
);
}
return errorMsg;
}
基于@Marc Schmid 的解决方案,如果您链接到 Sentry CDN 文件,我想出了以下工作示例。
<script src="https://browser.sentry-cdn.com/5.11.1/bundle.min.js" integrity="sha384-r7/ZcDRYpWjCNXLUKk3iuyyyEcDJ+o+3M5CqXP5GUGODYbolXewNHAZLYSJ3ZHcV" crossorigin="anonymous"></script>
<!-- https://github.com/getsentry/sentry-javascript/issues/1976#issuecomment-492260648 -->
<script src="https://browser.sentry-cdn.com/5.11.1/captureconsole.min.js"></script>
<script>
Sentry.init({
dsn: 'https://abcdef1234567890@sentry.io/012345',
debug: false,
integrations: [
new Sentry.Integrations.CaptureConsole({
levels: ['error']
})
],
});
</script>
找到了一个有点hacky的解决方案:
const consoleError = console.error;
console.error = function(firstParam) {
const response = consoleError.apply(console, arguments);
Raven.captureException(firstParam, { level: 'error' });
return response;
};
它只是包装了console.error
并将控制台中的每个错误日志报告给 Raven (Sentry)。
如果有人有更好的方法(可能是 Sentry 的一些隐藏功能),请随时分享!
我编写了一个使用您的 Sentry 实例进行此操作的库。 https://github.com/aneldev/dyna-sentry
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.