Symfony2:循环引用异常 - Monolog&定制异常处理程序

时间:2013-11-17 17:48:20

标签: php symfony monolog

尝试将Monolog电子邮件错误与自定义异常处理程序结合使用时,我得到以下结果:

  

[Symfony的\元器件\ DependencyInjection \异常\ ServiceCircularReferenceException]   检测到服务“路由器”的循环引用,路径:“router - >   monolog.logger.router - > monolog.handler.grouped - >   mana.exception.listener - >模板 - >树枝 - >   templating.helper.logout_url”。

我没有声称真的知道我在这里做了什么,正如所有错误所证明的那样。

服务摘录:

  mana.exception.listener:
    class:  Mana\ClientBundle\EventListener\ExceptionListener
    arguments: [@templating, @kernel]
    tags:
      - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

配置摘录:

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
            handler: grouped
        grouped:
            type: group
            members: [mail, custom]
        mail:
            type:         fingers_crossed
            action_level: error
            handler:      buffered
        buffered:
            type:    buffer
            handler: swift
        swift:
            type:       swift_mailer
            from_email: error@projectmana.org
            to_email:   truckeetrout@yahoo.com
            subject:    An Error Occurred!
            level:      debug
        custom:
            type: service
            id: mana.exception.listener

自定义处理程序:

class ExceptionListener {

    protected $templating;
    protected $kernel;

    public function __construct(EngineInterface $templating, $kernel) {
        $this->templating = $templating;
        $this->kernel = $kernel;
    }

    public function onKernelException(GetResponseForExceptionEvent $event) {
        // provide the better way to display a enhanced error page only in prod environment, if you want
        if ('prod' == $this->kernel->getEnvironment()) {
            // exception object
            $exception = $event->getException();

            // new Response object
            $response = new Response();

            // set response content
            $response->setContent(
                    // create you custom template AcmeFooBundle:Exception:exception.html.twig
                    $this->templating->render(
                            'ManaClientBundle:Exception:exception.html.twig', array('exception' => $exception)
                    )
            );

            // HttpExceptionInterface is a special type of exception
            // that holds status code and header details
            if ($exception instanceof HttpExceptionInterface) {
                $response->setStatusCode($exception->getStatusCode());
                $response->headers->replace($exception->getHeaders());
            } else {
                $response->setStatusCode(500);
            }

            // set the new $response object to the $event
            $event->setResponse($response);
        }
    }

2 个答案:

答案 0 :(得分:0)

这是你的异常侦听器。从arguments数组中删除@kernel,你很好

mana.exception.listener:
    class:  Mana\ClientBundle\EventListener\ExceptionListener
    arguments: [@templating]
    tags:
      - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

答案 1 :(得分:0)

您的自定义异常侦听器可能依赖于模板或内核服务中的monolog。