Sentry + Laravel:如何记录已经捕获的异常?

问题描述 投票:0回答:3

我们在很多项目中使用 Laravel(5.x 到 7.x),并且全部与 Sentry 集成。

在某些边缘情况下,我们需要捕获一些可恢复的异常,以允许页面流使用回退继续进行,但我们也希望将这些捕获的异常记录到哨兵。

我找不到手动登录 Sentry 的有记录或无记录的方法。

有没有办法将已经捕获的异常记录到哨兵?

php laravel sentry
3个回答
6
投票

^ 这是这样做的方法。

如果你想使用 Laravel 容器,它可能看起来更像这样:

try {
    // your code that could throw an exception
} catch (\Throwable $e) {
    if (app()->bound('sentry')) {
        app('sentry')->captureException($e);
    }
}

您还可以报告使用情况,这也应该将异常记录到您的日志文件中:

try {
    // your code that could throw an exception
} catch (\Throwable $e) {
    report($e); // this is a Laravel helper, not from Sentry
}

此外,您可以手动使用 Sentry 的助手 - 源代码在这里

\Sentry\captureMessage("This handle a text message");

// this handle everything derives from \Exception
\Sentry\captureException($e);

3
投票

这在文档中:https://docs.sentry.io/platforms/php/#capturing-errors

您可以使用

Sentry\captureException
方法:

try {
    $this->functionFailsForSure();
} catch (\Throwable $exception) {
    Sentry\captureException($exception);
}

0
投票

Duis adipisicing saedadsada reff2 2efavd rd

© www.soinside.com 2019 - 2024. All rights reserved.