如何用哨兵报告console.error?
How to report console.error with Sentry?
我的应用程序 console.error
报告了一些严重问题,但 thrown
没有,因此应用程序可能会继续 运行 - 可能处于瘫痪状态。
也有必要报告 console.error
问题,但是 Sentry (Raven) 库发送到服务器只抛出异常。
有人知道如何很好地解决这个问题吗?
(理想情况下无需重写所有 console.error
调用,因为某些供应商库可能仍将输出写入控制台)
找到了一个有点 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 的一些隐藏功能),请随时分享!
这是一个更强大的覆盖解决方案
// 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;
}
正如用户@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
一个新事件将发送到哨兵。
根据@Marc Schmid 的解决方案,如果您 link 到 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>
我使用您的 Sentry 实例编写了一个库。
https://github.com/aneldev/dyna-sentry
我的应用程序 console.error
报告了一些严重问题,但 thrown
没有,因此应用程序可能会继续 运行 - 可能处于瘫痪状态。
也有必要报告 console.error
问题,但是 Sentry (Raven) 库发送到服务器只抛出异常。
有人知道如何很好地解决这个问题吗?
(理想情况下无需重写所有 console.error
调用,因为某些供应商库可能仍将输出写入控制台)
找到了一个有点 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 的一些隐藏功能),请随时分享!
这是一个更强大的覆盖解决方案
// 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;
}
正如用户@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
一个新事件将发送到哨兵。
根据@Marc Schmid 的解决方案,如果您 link 到 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>
我使用您的 Sentry 实例编写了一个库。 https://github.com/aneldev/dyna-sentry