gcg-it/sentry-symfony

Sentry 的 Symfony 集成 (http://getsentry.com)

安装次数: 1,909

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 76

类型:symfony-bundle

2.0.5 2018-06-04 07:35 UTC

README

Sentry 的 集成

Stable release Unstable release

Build status Scrutinizer Coverage Status

优势

使用 sentry-symfony 进行

  • 快速设置 Sentry
  • 通过容器访问 sentry.client
  • 应用程序中的自动连接。每个事件都会自动添加以下内容
    • 用户
    • Symfony 环境
    • 应用程序路径
    • 主机名
    • 排除的路径(缓存和供应商)

安装

步骤 1: 下载 Bundle

打开命令控制台,进入项目目录,然后执行以下命令以下载此 Bundle 的最新稳定版本

$ composer require sentry/sentry-symfony

此命令要求您全局安装 Composer,请参阅 Composer 文档中的安装章节

步骤 2: 启用 Bundle

然后,通过将其添加到项目 app/AppKernel.php 文件中注册的 Bundle 列表来启用 Bundle

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
        );

        if (in_array($this->getEnvironment(), ['staging', 'prod'], true)) {
            $bundles[] = new Sentry\SentryBundle\SentryBundle();
        }
        // ...
    }

    // ...
}

请注意,使用此代码片段,此 Bundle 仅在 stagingprod 环境中启用;根据您的需要进行调整。不建议在 test 环境中启用此 Bundle,因为 Sentry 客户端将更改错误处理器,该处理器已被其他包(如 Symfony 的弃用处理器)使用(请参阅 #46#95)。

步骤 3: 配置 SDK

将项目的 Sentry DSN 值添加到 app/config/config.yml。留空此值将有效地禁用 Sentry 报告。

sentry:
    dsn: "https://public:secret@sentry.example.com/1"

维护版本

  • 2.x 在 master 分支上积极维护,但需要 Symfony 3+ 和 PHP 7.1+;
  • 1.x 仍然支持以允许 Symfony 2 和 PHP 5.6/7.0;它可能从 master 分支接收功能回滚,但不是保证的
  • 0.8.x 已不再维护,0.8.8 版本包含最新的新功能;未来可能只会接收安全修复。

配置

以下选项可以通过 app/config/config.yml 进行配置。

跳过一些异常

sentry:
    skip_capture:
        - "Symfony\Component\HttpKernel\Exception\HttpExceptionInterface"

监听器的优先级

您可以使用配置中的 listener_priorities 键更改此 Bundle 的 3 个默认监听器的优先级。默认值为 0,以下是 3 个可能的子键

listener_priorities:
    request: 0
    kernel_exception: 0
    console_exception: 0

分别对应于 onKernelRequestonKernelExceptiononConsoleException 事件。

选项

在以下部分,您将找到一些可以配置的选项,按字母顺序列出。所有可用的选项以及每个选项的更详细描述,可以在 Sentry 文档中找到,链接为 这里

app_path

您应用程序的基本路径。用于修剪前缀并将堆栈跟踪的帧标记为应用程序的一部分。

sentry:
    options:
        app_path: "/path/to/myapp"

environment

您的代码正在运行的环境(例如,生产环境)。

sentry:
    options:
        environment: "%kernel.environment%"

错误类型

定义应报告哪些错误类型。

sentry:
    options:
        error_types: E_ALL & ~E_DEPRECATED & ~E_NOTICE

exception_listener

这用于替换此包使用的默认异常监听器。值必须是实现 SentryExceptionListenerInterface 接口类的 FQCN。有关更多详细信息,请参阅 创建自定义 ExceptionListener

sentry:
    exception_listener: AppBundle\EventListener\MySentryExceptionListener

prefixes

要从文件名中剥离的前缀列表。这些通常是供应商的 include 路径。

sentry:
    options:
        prefixes:
            - /usr/lib/include

release

您应用程序的版本。通常这是提交的 Git SHA 哈希值。

sentry:
    options:
        release: "beeee2a06521a60e646bbb8fe38702e61e4929bf"

tags

为记录的错误定义标签。

sentry:
    options:
        tags:
            tag1: tagvalue
            tag2: tagvalue

已弃用的配置选项

在此包的先前版本中,直到 0.8.2,一些先前选项被设置在配置文件的选项级别之外。这些选项仍然有效,但已弃用,并且在稳定的 1.x 版本中将被删除,因此 建议您放弃使用它们;为了提供向前兼容性,它们仍然可以与标准语法一起使用,但值必须匹配。以下是这些选项的列表

sentry:
    app_path: ~
    environment: ~
    error_types: ~
    excluded_app_paths: ~
    prefixes: ~
    release: ~

自定义

您可以对用户上下文的配置进行自定义,以及通过将事件订阅者连接到默认配置的 ExceptionListener 发射的事件来修改客户端,在捕获异常之前立即进行修改(或者,您也可以仅定义自己的自定义异常监听器)。

创建自定义 ExceptionListener

您始终可以用自己的自定义监听器替换默认的 ExceptionListener。为此,将不同的类分配给 Sentry 配置中的 exception_listener 属性,例如:

sentry:
    options:
        exception_listener: AppBundle\EventListener\MySentryExceptionListener

... 然后定义自定义的 ExceptionListener,该监听器实现了 SentryExceptionListenerInterface,例如:

// src/AppBundle/EventSubscriber/MySentryEventListener.php
namespace AppBundle\EventSubscriber;

use Sentry\SentryBundle\EventListener\SentryExceptionListenerInterface;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class MySentryExceptionListener implements SentryExceptionListenerInterface
{
    // ...

    public function __construct(TokenStorageInterface $tokenStorage = null, AuthorizationCheckerInterface $authorizationChecker = null, \Raven_Client $client = null, array $skipCapture, EventDispatcherInterface $dispatcher = null)
    {
        // ...
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        // ...
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // ...
    }

    public function onConsoleException(ConsoleExceptionEvent $event)
    {
        // ...
    }
}

作为附带说明,虽然上面的示例演示了一个不扩展任何内容的自定义异常监听器,但您可以选择扩展默认的 ExceptionListener 并仅覆盖您想要的功能。

为 Sentry 事件添加事件订阅者

创建一个新类,例如 MySentryEventSubscriber

// src/AppBundle/EventSubscriber/MySentryEventListener.php
namespace AppBundle\EventSubscriber;

use Sentry\SentryBundle\Event\SentryUserContextEvent;
use Sentry\SentryBundle\SentrySymfonyEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MySentryEventSubscriber implements EventSubscriberInterface
{
    /** @var \Raven_Client */
    protected $client;

    public function __construct(\Raven_Client $client)
    {
        $this->client = $client;
    }

    public static function getSubscribedEvents()
    {
        // return the subscribed events, their methods and priorities
        return array(
            SentrySymfonyEvents::PRE_CAPTURE => 'onPreCapture',
            SentrySymfonyEvents::SET_USER_CONTEXT => 'onSetUserContext'
        );
    }

    public function onSetUserContext(SentryUserContextEvent $event)
    {
        // ...
    }

    public function onPreCapture(Event $event)
    {
        if ($event instanceof GetResponseForExceptionEvent) {
            // ...
        }
        elseif ($event instanceof ConsoleExceptionEvent) {
            // ...
        }
    }
}

在上面的例子中,如果您订阅了PRE_CAPTURE事件,您可能会接收到一个事件对象,这个对象更偏向于对网络请求的响应(例如GetResponseForExceptionEvent)或是在命令行中采取的操作(例如ConsoleExceptionEvent)。根据代码是如何被调用的,以及您是否需要在预捕获期间区分这些事件,您最好在处理该对象之前先测试事件的类型(如上面所示)。

要配置上述功能,请将以下配置添加到您的服务定义中

app.my_sentry_event_subscriber:
    class: AppBundle\EventSubscriber\MySentryEventSubscriber
    arguments:
      - '@sentry.client'
    tags:
      - { name: kernel.event_subscriber }