macromindonline / sentry-laravel
Sentry (https://sentry.io) 的 Laravel 集成
Requires
- php: >=5.3.0
- illuminate/support: 4.*|5.*
- sentry/sentry: >=1.7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.8.0
- orchestra/testbench: 3.*
- phpunit/phpunit: ^4.6.6
This package is not auto-updated.
Last update: 2024-10-01 18:04:12 UTC
README
Sentry for Laravel
Laravel 的 Sentry 集成(Sentry)。
安装
Laravel 5.x
安装 sentry/sentry-laravel 包
$ composer require sentry/sentry-laravel
如果你使用的是 Laravel 5.4 或更早版本,需要将以下内容添加到你的 config/app.php 文件中
'providers' => array( // ... Sentry\SentryLaravel\SentryLaravelServiceProvider::class, ) 'aliases' => array( // ... 'Sentry' => Sentry\SentryLaravel\SentryFacade::class, )
将 Sentry 报告添加到 app/Exceptions/Handler.php
public function report(Exception $exception) { if (app()->bound('sentry') && $this->shouldReport($exception)) { app('sentry')->captureException($exception); } parent::report($exception); }
创建 Sentry 配置文件(config/sentry.php)
$ php artisan vendor:publish --provider="Sentry\SentryLaravel\SentryLaravelServiceProvider"
将你的 DSN 添加到 .env
SENTRY_DSN=https://public:secret@sentry.example.com/1
Laravel 4.x
安装 sentry/sentry-laravel 包
$ composer require sentry/sentry-laravel
在 config/app.php 中添加 Sentry 服务提供者和外观
'providers' => array( // ... 'Sentry\SentryLaravel\SentryLaravelServiceProvider', ) 'aliases' => array( // ... 'Sentry' => 'Sentry\SentryLaravel\SentryFacade', )
创建 Sentry 配置文件(config/sentry.php)
$ php artisan config:publish sentry/sentry-laravel
Lumen 5.x
安装 sentry/sentry-laravel 包
$ composer require sentry/sentry-laravel
在 bootstrap/app.php 中注册 Sentry
$app->register('Sentry\SentryLaravel\SentryLumenServiceProvider'); # Sentry must be registered before routes are included require __DIR__ . '/../app/Http/routes.php';
将 Sentry 报告添加到 app/Exceptions/Handler.php
public function report(Exception $e) { if (app()->bound('sentry') && $this->shouldReport($e)) { app('sentry')->captureException($e); } parent::report($e); }
创建 Sentry 配置文件(config/sentry.php)
<?php return array( 'dsn' => '___DSN___', // capture release as git sha // 'release' => trim(exec('git log --pretty="%h" -n1 HEAD')), // Capture bindings on SQL queries 'breadcrumbs.sql_bindings' => true, // Capture default user context 'user_context' => true, );
使用 Artisan 测试
你可以使用提供的 artisan 命令来测试你的配置
$ php artisan sentry:test [sentry] Client configuration: -> server: https://app.getsentry.com/api/3235/store/ -> project: 3235 -> public_key: e9ebbd88548a441288393c457ec90441 -> secret_key: 399aaee02d454e2ca91351f29bdc3a07 [sentry] Generating test event [sentry] Sending test event with ID: 5256614438cf4e0798dc9688e9545d94
添加上下文
添加上下文机制的细节将取决于你使用的 Laravel 版本,但基本方法相同。找到应用程序中可以获取你想要添加的上下文的位置,理想情况下在早期阶段。
以下示例中,我们将使用一个中间件
namespace App\Http\Middleware; use Closure; class SentryContext { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * * @return mixed */ public function handle($request, Closure $next) { if (app()->bound('sentry')) { /** @var \Raven_Client $sentry */ $sentry = app('sentry'); // Add user context if (auth()->check()) { $sentry->user_context([...]); } else { $sentry->user_context(['id' => null]); } // Add tags context $sentry->tags_context([...]); } return $next($request); } }
显示错误 ID
当发生错误并且你在邮箱中收到客户邮件时,如果他们能提供一些标识错误的标识符,那就很好了。
幸运的是,Sentry 通过在你的错误视图中添加以下选项之一为你提供了这样的功能。
// Using the Sentry facade $errorID = Sentry::getLastEventID(); // Or without the Sentry facade (Lumen) $errorID = app('sentry')->getLastEventID();
例如,在你的 resources/views/error/500.blade.php 中,它可能看起来像这样
@if(!empty(Sentry::getLastEventID())) <p>Please send this ID with your support request: {{ Sentry::getLastEventID() }}.</p> @endif
这个 ID 可以在 Sentry 界面中进行搜索,这样你可以快速找到错误。
贡献
依赖项通过 composer 管理
$ composer install
然后可以通过 phpunit 运行测试
$ vendor/bin/phpunit