我有一个应用程序,其中一些关键问题是使用 console.error
报告的,但并没有被 thrown
,因此应用程序可能会继续运行 - 可能处于瘫痪状态。
需要报告 console.error
问题,但 Sentry(Raven)库只向服务器发送抛出的异常。
是否有人知道如何优雅地解决这个问题?
(最好不需要重写所有 console.error
调用,因为某些供应商库仍然可能将输出写入控制台)
我有一个应用程序,其中一些关键问题是使用 console.error
报告的,但并没有被 thrown
,因此应用程序可能会继续运行 - 可能处于瘫痪状态。
需要报告 console.error
问题,但 Sentry(Raven)库只向服务器发送抛出的异常。
是否有人知道如何优雅地解决这个问题?
(最好不需要重写所有 console.error
调用,因为某些供应商库仍然可能将输出写入控制台)
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
,将会发送一个新的事件到Sentry。CaptureConsole
后才是真的。 - Advena这是一个更加强大的覆盖解决方案
// 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>
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)控制台。我查看了文档,截至今天,您也可以使用 attachStacktrace: true
来包含堆栈跟踪,因此代码将如下所示:
<script src="https://browser.sentry-cdn.com/7.49.0/bundle.min.js" crossorigin="anonymous"></script>
<script src="https://browser.sentry-cdn.com/7.49.0/captureconsole.min.js" crossorigin="anonymous"></script>
<script>
Sentry.init({
dsn: 'https://abcdef1234567890@sentry.io/012345',
environment: 'production',
integrations: [ new Sentry.Integrations.CaptureConsole({ levels: ['error'] }) ],
attachStacktrace: true,
});
</script>