[英]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.