avto-dev / sentry-laravel
Requires
- php: ^7.2
- illuminate/container: ~6.0 || ~7.0 || ~8.0
- illuminate/support: ~6.0 || ~7.0 || ~8.0
- sentry/sentry-laravel: ^1.6
Requires (Dev)
- avto-dev/app-version-laravel: ^2.1 || ~3.0
- laravel/laravel: ~6.0 || ~7.0 || ~8.0
- mockery/mockery: ^1.3
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8.5.4 || ^9.3
Suggests
- avto-dev/app-version-laravel: Adds release version into sentry events (^3.0)
README
Sentry for Laravel应用程序
此包允许您
安装
使用以下命令使用 composer 安装此包
$ composer require avto-dev/sentry-laravel "^2.3"
需要安装
composer
(如何安装 composer)。
您需要修复包的主版本。
将 Sentry 报告添加到 ./app/Exceptions/Handler.php
<?php namespace App\Exceptions; class Handler extends \Illuminate\Foundation\Exceptions\Handler { // ... /** * Report or log an exception. * * @param \Exception $exception * * @return void */ public function report(\Exception $exception): void { if ($this->container->bound('sentry') && $this->shouldReport($exception)) { try { $this->container->make('sentry')->captureException($exception); } catch (\Exception $e) { $this->container->make(\Psr\Log\LoggerInterface::class)->error( 'Cannot capture exception with sentry: ' . $e->getMessage(), ['exception' => $e] ); } } parent::report($exception); } // ... }
使用此命令创建 Sentry 配置文件(./config/sentry.php
)
如果您已经存在
./config/sentry.php
文件 - 使用以下命令重命名它$ test -f ./config/sentry.php && mv ./config/sentry.php ./config/sentry.php.old
$ php artisan vendor:publish --tag=sentry-config --force
并按需编辑它。
使用 Artisan 进行测试
您可以使用提供的 artisan
命令测试您的配置。
$ php artisan sentry:test [sentry] Client DSN discovered! [sentry] Generating test event [sentry] Sending test event [sentry] Event sent: e6442bd7806444fc8b2710abce3599ac
本地开发
当 Sentry 安装在您的应用程序中时,在您进行开发时它也会处于活动状态。
如果您不想在开发时将错误发送到 Sentry,请将 DSN 值设置为 null
(在您的 .env
文件中定义 SENTRY_LARAVEL_DSN=null
)。
使用 Laravel 日志通道
注意:如果您正在使用日志通道记录您的异常,并且也在异常处理程序中记录异常到 Sentry(如您上面配置的),异常最终可能会在 Sentry 中出现两次
要将 Sentry 配置为日志通道,请将以下配置添加到 ./config/logging.php
中的 channels
部分
<?php return [ 'channels' => [ // ... 'sentry' => [ 'driver' => 'sentry', ], ], ];
在您配置了 Sentry 日志通道之后,您可以修改日志堆栈以使您的应用程序既能记录到日志文件也能记录到 Sentry
<?php return [ 'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => ['single', 'sentry'], // Add the Sentry log channel to the stack ], // ... ], ];
可选地,您可以设置日志级别以及是否在驱动程序上冒泡事件
并修改以下行
<?php return [ 'channels' => [ // ... 'sentry' => [ 'driver' => 'sentry', 'level' => null, // The minimum monolog logging level at which this handler will be triggered // For example: `\Monolog\Logger::ERROR` 'bubble' => true, // Whether the messages that are handled can bubble up the stack or not ], ], ];
命名您的日志通道
如果您想在内置的 Sentry 界面中过滤多个日志通道,您可以将 name
属性添加到日志通道。它将显示为 logger
标签,可进行筛选。
例如
<?php return [ 'channels' => [ // ... 'my_stacked_channel' => [ 'driver' => 'stack', 'channels' => ['single', 'sentry'], 'name' => 'my-channel' ], ], ];
您现在可以将其错误记录到您的通道
<?php \Illuminate\Support\Facades\Log::channel('my_stacked_channel')->error('My error');
并且 Sentry 的 logger
标签现在有通道的 name
。您可以筛选“my-channel”值。
测试
对于包测试,我们使用 phpunit
框架和 docker-ce
+ docker-compose
作为开发环境。因此,在仓库克隆后,只需在终端中写入
$ make build $ make latest # or 'make lowest' $ make test
更改日志
变更日志可以在这里找到。
支持
如果您发现任何包的错误,请在当前仓库中提交一个issue。
许可证
这是一个开源软件,采用MIT许可证。