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

Sentry + Laravel: how to log an already catched Exception?

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

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

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

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

^ 这是方法。

如果您想使用 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
}

此外,您可以手动使用哨兵的助手 - the source code is here

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

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

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

您可以使用Sentry\captureException方法:

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