Sentry-javascript: 如何将异常(`captureException`)也记录到控制台

创建于 2018-09-30  ·  17评论  ·  资料来源: getsentry/sentry-javascript

当我在本地开发时,我禁用向 Sentry 发送错误,但我仍然希望这些错误显示在我的控制台中,以便我可以修复它们。

有没有办法禁用向 Sentry 发送异常/事件但仍将它们记录到控制台?

最有用的评论

@gaastonsr你做的第二个参数 :) https://docs.sentry.io/learn/filtering/?platform=browser#before -send

Sentry.init({
  dsn: "DSN",
  beforeSend: (event, hint) => {
   if (IS_DEBUG) {
     console.error(hint.originalException || hint.syntheticException);
     return null; // this drops the event and nothing will be sent to sentry
   }
   return event;
  }
});

所有17条评论

出于这个原因,我们包装了captureException

function captureException(...args) {
    if (typeof Sentry !== 'undefined') {
        Sentry.captureException(...args);
    }
    else {
        console.error(...args);
    }
}

我会说最好的方法是使用beforeSend
```js
哨兵.init({
dsn: "DSN",
beforeSend: 事件 => {
如果(IS_DEBUG){
控制台错误(事件);
返回空; // 这会删除事件,并且不会向哨兵发送任何内容
}
返回事件;
}
});

我不喜欢使用beforeSend是我没有得到原始的错误对象。 不过谢谢你的回复。

@gaastonsr你做的第二个参数 :) https://docs.sentry.io/learn/filtering/?platform=browser#before -send

Sentry.init({
  dsn: "DSN",
  beforeSend: (event, hint) => {
   if (IS_DEBUG) {
     console.error(hint.originalException || hint.syntheticException);
     return null; // this drops the event and nothing will be sent to sentry
   }
   return event;
  }
});

天哪,这改变了一切! 谢谢,@kamilogorek。

我花了 30 分钟才找到为什么我的控制台没有记录错误。 这应该更容易做到。

这里的问题是,如果我们默认记录它,我们也会默认将其捕获为面包屑。 我们已经为捕获错误执行了此操作。 因此它会创建重复的条目。
尽管我对反馈完全敞开心扉,如果有人知道如何改进这个文档部分,我很乐意把它包括在内:)

这里的问题是,如果我们默认记录它,我们也会默认将其捕获为面包屑。 我们已经为捕获错误执行了此操作。 因此它会创建重复的条目。
尽管我对反馈完全敞开心扉,如果有人知道如何改进这个文档部分,我很乐意把它包括在内:)

哨兵捕获错误并且没有在控制台中显示它们真的是出乎意料的行为!
我还花了一个小时来发现为什么我的应用程序不起作用,尽管控制台很清晰。
这绝对应该改变。

我认为您可以从服务器端的面包屑中过滤掉错误。
或者可能在setTimeout回调中将其记录到控制台,因此面包屑已经被捕获。 正如我目前所做的:

Sentry.init({
  dsn: 'DSN',
  beforeSend: (event, hint) => {
    setTimeout(() => console.error(hint.originalException || hint.syntheticException), 0);
    return event;
  }
});

@vitalets它已在5.9.0更改。 您现在也会在控制台中看到错误。

@vitalets它已在5.9.0更改。 您现在也会在控制台中看到错误。

@kamilogorek
它在sentry/browser 5.9.1 (chrome 78, osx) 上对我不起作用。
这是代码:

Sentry.init({  dsn: 'dsn' });
setTimeout(() => {throw new Error('abc')}, 3000);

控制台是空的。 网络选项卡显示错误已发送到哨兵。

没有哨兵错误显示:

// Sentry.init({  dsn: 'dsn' });
setTimeout(() => {throw new Error('abc')}, 3000);

image

它记录了 captureException 而不是捕获事件:
为了记录 captureEvent 和 captureException 我们稍微修改了@kamilogorek的解决方案:

Sentry.init({
  dsn: "DSN",
  beforeSend: (event, hint) => {
   if (IS_DEBUG) {
     console.error(hint.originalException || hint.syntheticException || event);
     return null; // this drops the event and nothing will be sent to sentry
   }
   return event;
  }
});

@kamilogorek也是@sentry/node包的默认设置吗? 我有5.9.0版本,我看到beforeSend函数被调用,但我在控制台中没有看到任何记录。

我在 5.10.1 的控制台上也没有看到任何错误。 如果 debug 设置为 true,日志记录就足够了。

这就是当你没有读到最后的规范时会发生的事情......

https://html.spec.whatwg.org/multipage/webappapis.html#the -event-handler-processing-algorithm

如果特殊错误事件处理为真
如果返回值为真,则设置事件的取消标志。
除此以外
如果返回值为 false,则设置事件的取消标志。

然后你滚动...

由于历史原因,平台中有两个例外:
全局对象上的 onerror 处理程序,返回 true 取消事件

我会相应地更新代码

当你不阅读规范时会发生这种情况

没有人读到最后的规格:)

谢谢你的修复!

如果我正确阅读了这个线程,那么当前版本的哨兵应该在控制台中显示错误,同时还将它们发送到哨兵仪表板。 我没有遇到这种行为。

这是我如何将哨兵加载到我的页面中:

<script src="https://browser.sentry-cdn.com/5.20.1/bundle.min.js" integrity="sha384-O8HdAJg1h8RARFowXd2J/r5fIWuinSBtjhwQoPesfVILeXzGpJxvyY/77OaPPXUo" crossorigin="anonymous"></script>
<script src="https://browser.sentry-cdn.com/5.20.1/vue.min.js" crossorigin="anonymous"></script>

这是我的初始化调用:

  Sentry.init({
    dsn: 'https://_________.ingest.sentry.io/___________',
    integrations: [new Sentry.Integrations.Vue({Vue, attachProps: true})],
  });

我添加了以下错误产生行:

 window.undef.undef += 1;

当我加载页面并触发错误行时,控制台中没有任何显示,但我确实在哨兵仪表板中看到了错误。 如果我注释掉Sentry.init调用,那么我会在 js 控制台中看到TypeError

我期望基于我对这个线程的阅读,我会在 js 控制台中看到错误,同时还将其记录到哨兵仪表板中。 那不正确吗? 我还需要使用beforeSend钩子吗?

我现在意识到我需要将logErrors: true传递给Integrations.Vue调用:

new Sentry.Integrations.Vue({Vue, attachProps: true, logErrors: true}

抱歉,我应该更仔细地阅读文档!

此页面是否有帮助?
0 / 5 - 0 等级