title: Symfony2: Circular reference exception - Monolog & custom exception handler
body: <p>In an attempt to combine Monolog e-mailing errors with a custom exception handler I get the following:</p>
  
  <blockquote>
    <p>[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]
    Circular reference detected for service "router", path: "router ->
    monolog.logger.router -> monolog.handler.grouped ->
    mana.exception.listener -> templating -> twig ->
    templating.helper.logout_url".</p>
  </blockquote>
  
  <p>I make no claims to really know what I'm doing here, as evidenced by all the errors.</p>
  
  <h3>Services excerpt:</h3>
  
  <pre><code>  mana.exception.listener:
      class:  Mana\ClientBundle\EventListener\ExceptionListener
      arguments: [@templating, @kernel]
      tags:
        - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
  </code></pre>
  
  <h3>Config excerpt:</h3>
  
  <pre><code>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
  </code></pre>
  
  <h3>Custom handler:</h3>
  
  <pre><code>class ExceptionListener {
  
      protected $templating;
      protected $kernel;
  
      public function __construct(EngineInterface $templating, $kernel) {
          $this-&gt;templating = $templating;
          $this-&gt;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-&gt;kernel-&gt;getEnvironment()) {
              // exception object
              $exception = $event-&gt;getException();
  
              // new Response object
              $response = new Response();
  
              // set response content
              $response-&gt;setContent(
                      // create you custom template AcmeFooBundle:Exception:exception.html.twig
                      $this-&gt;templating-&gt;render(
                              'ManaClientBundle:Exception:exception.html.twig', array('exception' =&gt; $exception)
                      )
              );
  
              // HttpExceptionInterface is a special type of exception
              // that holds status code and header details
              if ($exception instanceof HttpExceptionInterface) {
                  $response-&gt;setStatusCode($exception-&gt;getStatusCode());
                  $response-&gt;headers-&gt;replace($exception-&gt;getHeaders());
              } else {
                  $response-&gt;setStatusCode(500);
              }
  
              // set the new $response object to the $event
              $event-&gt;setResponse($response);
          }
      }
  </code></pre>