onlime/exception-report-bundle

该包在您的Symfony3项目中启用简单的通过电子邮件发送的异常报告。

0.3.5 2016-05-10 07:52 UTC

This package is auto-updated.

Last update: 2024-08-29 04:14:31 UTC


README

此包将自动加载异常处理器到框架中,以便所有未捕获的错误都通过电子邮件发送给开发者。无需外部服务。仅生成包括重要的客户端和请求元数据的简单异常报告。

此包受到PhpAirbrakeBundle的启发。

要求

安装

使用Composer安装

$ composer require onlime/exception-report-bundle

更新GeoIP数据库

$ bin/console maxmind:geoip:update-data http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

配置

在AppKernel.php中启用该包

# app/AppKernel.php
<?php
    // ...
    public function registerBundles()
    {
        $bundles = array(
        	// ...
            new Onlime\ExceptionReportBundle\OnlimeExceptionReportBundle(),
        );
	}

在config.yml中配置该包

# app/config/config.yml

onlime_exception_report:
    handlers:
        swift:
            # by default 4xx http exceptions are not reported:
            # exclude_http: true
            #
            # to also log 4xx level errors (but not 404's):
            # exclude_http: false
            # excluded_404s:
            #     - ^/
            #
            # to hide stacktrace output for specific exceptions,
            # add them with fully qualified class name:
            # no_stacktrace_on_exceptions:
            #     - Doctrine\DBAL\Exception\ConnectionException
            #     - PDOException
            #
            exclude_http: false
            from_email: '%email.admin%'
            to_email:   '%email.admin%'
            subject:    "[Example.com] Exception Report"

您可能需要在开发环境中禁用特定的处理器

# app/config/config_dev.yml

onlime_exception_report:
    handlers:
        swift:
            enabled: false

默认配置参考

# Default configuration for extension with alias: "onlime_exception_report"
onlime_exception_report:
    handlers:

        # Prototype
        name:
            enabled:              true
            exclude_http:         true
            excluded_404s:        []
            no_stacktrace_on_exceptions:  []
            from_email:           ~ # Required
            to_email:             [] # Required
            subject:              'Exception Report'

此默认配置参考是通过以下命令生成的

$ bin/console config:dump-reference onlime_exception_report

示例报告

在控制器中简单地抛出异常,例如

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
 * @Route("/demo")
 */
class DemoController extends Controller
{
    /**
     * Simply throws an Exception to test exception reporting.
     *
     * @Route("/exception", name="demo_exception")
     */
    public function exceptionAction()
    {
        throw new \Exception('Something really bad happened!');
    }
}

然后您将在网络调试工具栏中找到异常报告或在正确配置的Swift mailer中在您的邮箱中找到它

GET /demo/exception: Something really bad happened!
Exception: Exception

Date:       Sun, 28 Feb 2016 22:15:47 +0100
ClientIP:   212.51.128.110 (CH / Switzerland)
ClientHost: 212-51-128-110.fiber7.init7.net
ReqHost:    demo.example.com
ReqUri:     http://demo.example.com/demo/exception
Env:        prod
Route:      demo_exception
User:       onlime
Agent:      Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:49.0) Gecko/20100101 Firefox/49.0
Referer:

------------------------------------------------------------
Stack trace
------------------------------------------------------------
#0 [internal function]: AppBundle\Controller\DemoController->exceptionAction()
#1 vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php(139): call_user_func_array(Array, Array)
#2 vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php(62): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#3 vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(169): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#4 web/app.php(58): Symfony\Component\HttpKernel\Kernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#5 {main}

------------------------------------------------------------
Request Parameters
------------------------------------------------------------

POST Array
(
)


GET Array
(
)

特殊用例

触发异常报告而不中断应用程序

假设您捕获了一个异常(例如来自API请求)并希望不中断应用程序流程,但仍然想报告完整的异常。这就是ExceptionEvent的作用——只需像这样在控制器中触发它即可

<?php
use Onlime\ExceptionReportBundle\Event\ExceptionEvent;
use Onlime\ExceptionReportBundle\Event\ExceptionEvents;

// ...

        try {
            // do some dangerous stuff here...
        } catch (\Exception $e) {
            $this->get('event_dispatcher')->dispatch(
                ExceptionEvents::REPORT,
                new ExceptionEvent($e)
            );
        }

作者

版权(c)2016 Onlime Webhosting www.onlime.ch(Twitter @ondalime

许可

此包在MIT许可下发布。

致谢

此包受到ibrows/PhpAirbrakeBundle的启发,并使用其中的一些代码,该包是dbtlr/PhpAirbrakeBundle的分支。

我还想感谢Mike Meier的建议。