abenevaut / laravel-sentry-handler
Laravel sentry 异常处理器
1.0.2
2024-04-14 12:02 UTC
Requires
- php: ^8.1
- sentry/sentry-laravel: ^2.12 || ^3.0 || ^4.0
Requires (Dev)
- degraciamathieu/php-smelly-code-detector: ^1.1
- icanhazstring/composer-unused: ^0.8.11
- laravel/framework: ^9.17 || ^10 || ^11
- mockery/mockery: ^1.5
- phpunit/phpunit: ^10.1
- squizlabs/php_codesniffer: ^3.8
This package is auto-updated.
Last update: 2024-09-14 12:46:48 UTC
README
该包方便将Sentry与上下文范围异常集成,当发生异常时能够传输数据。
安装
composer require abenevaut/laravel-sentry-handler php artisan sentry:publish --dsn=
使用方法
更新ExceptionHandler
本包中封装的异常供应商能够向Sentry报告。因为我们可能想要将所有异常报告给Sentry,所以我们能够实现 $this->reportSentry($e);
将它们记录到Sentry。
继承的处理器
在 app/Exceptions/Handler.php
中,将 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
替换为 use abenevaut\SentryHandler\Handler as ExceptionHandler;
如果您已经自定义了异常处理器,请确保调整您的 report()
方法
public function report(\Throwable $e): void { // Report standard exceptions to sentry $this->reportSentry($e); parent::report($e); }
注意:该方法在 示例 中使用
处理器特质
在 app/Exceptions/Handler.php
中,为 App\Exceptions\Handler
类添加 use SentryHandlerTrait;
然后调整您的 report()
方法
public function report(\Throwable $e): void { // Report standard exceptions to sentry $this->reportSentry($e); parent::report($e); }
使用标准异常测试Sentry
php artisan sentry:test
范围异常
Laravel ExceptionHandler 允许通过实现 report()
方法让异常自我报告。我们使用这个地方来计算异常上下文,并将其抛给Sentry。
final class MyException extends \abenevaut\SentryHandler\Contracts\ExceptionAbstract { /** * @var array|string[] */ private array $scopes = [ /* * Context always reported */ DefaultScope::class, ]; } $exception = new MyException(); // Depending context, add relative scope $exception->addScope( DefaultScope::class ); // You can also pass an instantiated object, if you required to compute something $exception->addScope( new DefaultScope( ... ) ); report($exception);
- 设置异常严重性
// incoming soon
- 发送Sentry消息
// incoming soon
什么是范围
final class DefaultScope extends \abenevaut\SentryHandler\Contracts\ScopeAbstract { public function handle(Scope $scope, Closure $next) { /* * Stack context in Sentry scope. * @seealso https://docs.sentry.io/platforms/php/guides/laravel/enriching-events/?original_referrer=https%3A%2F%2Fwww.google.com%2F */ $scope ->setUser([ // ... ]) ->setTags([ // ... ]); return $next($scope); } }
测试
vendor/bin/smelly-code-detector inspect src vendor/bin/phpunit