rsands2801/sentry-laravel

Laravel对Sentry(https://sentry.io)的集成

0.6.1 2017-02-09 17:35 UTC

README

Laravel对Sentry的集成。

Laravel 5.x

安装sentry/sentry-laravel

$ composer require sentry/sentry-laravel

config/app.php中添加Sentry服务提供者和外观

'providers' => array(
    // ...
    Sentry\SentryLaravel\SentryLaravelServiceProvider::class,
)

'aliases' => array(
    // ...
    'Sentry' => Sentry\SentryLaravel\SentryFacade::class,
)

app/Exceptions/Handler.php中添加Sentry报告

public function report(Exception $exception)
{
    if ($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';

app/Exceptions/Handler.php中添加Sentry报告

public function report(Exception $e)
{
    if ($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,
);

添加上下文

添加上下文的机制将根据您使用的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);
    }
}

贡献

首先,请确保您能够运行测试套件。安装开发依赖项

$ composer install

您现在可以使用phpunit

$ vendor/bin/phpunit

资源