From 25e32c66c02835274b61b902d1147f9770501e3b Mon Sep 17 00:00:00 2001 From: Moises Burgos Date: Wed, 8 Jul 2015 16:36:17 +1200 Subject: [PATCH 1/3] Example of handling exceptions with listeners --- cookbook/controller/error_pages.rst | 121 ++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/cookbook/controller/error_pages.rst b/cookbook/controller/error_pages.rst index a76fd767d24..d97c09c5cf2 100644 --- a/cookbook/controller/error_pages.rst +++ b/cookbook/controller/error_pages.rst @@ -250,6 +250,127 @@ This approach allows you to create centralized and layered error handling: instead of catching (and handling) the same exceptions in various controllers time and again, you can have just one (or several) listeners deal with them. +A centralized error handling that also allows to log exceptions might look like this:: + + // src/AppBundle/EventListener/AcmeExceptionListener.php + namespace AppBundle\EventListener; + + use Psr\Log\LoggerInterface; + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; + use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; + + class AcmeExceptionListener + { + private $logger; + + public function __construct(LoggerInterface $logger) + { + $this->logger = $logger; + } + + public function onKernelException(GetResponseForExceptionEvent $event) + { + // Get the exception object from the received event + $exception = $event->getException(); + $message = sprintf( + 'Error: %s. Code: %s', + $exception->getMessage(), + $exception->getCode() + ); + + // Log the exception to the acme.log file + $this->logger->error($message); + + // Customize the response object to display the exception details + $response = new Response(); + $response->setContent($message); + + // 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(Response::HTTP_INTERNAL_SERVER_ERROR); + } + + // Send the modified response object to the event + $event->setResponse($response); + } + } + +Now, we need to register our listener: + +.. configuration-block:: + + .. code-block:: yaml + + # app/config/services.yml + services: + kernel.listener.your_exception_listener_name: + class: AppBundle\EventListener\AcmeExceptionListener + arguments: ["@logger"] + tags: + - { name: kernel.event_listener, event: kernel.exception, method: onKernelException } + + .. code-block:: xml + + + + + + + + .. code-block:: php + + // app/config/services.php + $container + ->register('kernel.listener.your_exception_listener_name', 'AppBundle\EventListener\AcmeExceptionListener') + ->addArgument(new Reference('logger')) + ->addTag('kernel.event_listener', array('event' => 'kernel.exception', 'method' => 'onKernelException')) + ; + +Finally, we need to add a new handler that will log the exceptions to the acme.log file: + +.. configuration-block:: + + .. code-block:: yaml + + # app/config/config.yml + monolog: + handlers: + acmelog: + type: stream + path: /path/to/acme.log + level: error + + .. code-block:: xml + + + + + + + + .. code-block:: php + + // app/config/config.php + $container->loadFromExtension('monolog', array( + 'handlers' => array( + 'acmelog' => array( + 'type' => 'stream', + 'path' => '/path/to/acme.log', + 'level' => 'error', + ), + ), + )); + .. tip:: See :class:`Symfony\\Component\\Security\\Http\\Firewall\\ExceptionListener` From ddeaa7a622122c7019de9e4831ab60093d00fe9b Mon Sep 17 00:00:00 2001 From: mdburgos Date: Thu, 9 Jul 2015 21:34:01 +1200 Subject: [PATCH 2/3] Update error_pages.rst --- cookbook/controller/error_pages.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cookbook/controller/error_pages.rst b/cookbook/controller/error_pages.rst index d97c09c5cf2..4edb71d8f57 100644 --- a/cookbook/controller/error_pages.rst +++ b/cookbook/controller/error_pages.rst @@ -279,7 +279,7 @@ A centralized error handling that also allows to log exceptions might look like $exception->getCode() ); - // Log the exception to the acme.log file + // Log the exception to the configured log file (i.e. acme.log) $this->logger->error($message); // Customize the response object to display the exception details @@ -308,7 +308,7 @@ Now, we need to register our listener: # app/config/services.yml services: - kernel.listener.your_exception_listener_name: + app.exception_listener: class: AppBundle\EventListener\AcmeExceptionListener arguments: ["@logger"] tags: @@ -331,7 +331,7 @@ Now, we need to register our listener: ->addTag('kernel.event_listener', array('event' => 'kernel.exception', 'method' => 'onKernelException')) ; -Finally, we need to add a new handler that will log the exceptions to the acme.log file: +Finally, we need to add a new handler that will log the exceptions to the configured log file (acme.log): .. configuration-block:: @@ -340,7 +340,7 @@ Finally, we need to add a new handler that will log the exceptions to the acme.l # app/config/config.yml monolog: handlers: - acmelog: + applog: type: stream path: /path/to/acme.log level: error @@ -350,7 +350,7 @@ Finally, we need to add a new handler that will log the exceptions to the acme.l loadFromExtension('monolog', array( 'handlers' => array( - 'acmelog' => array( + 'applog' => array( 'type' => 'stream', 'path' => '/path/to/acme.log', 'level' => 'error', From b77a1614e254dae05fd8d45d542eec709fe0dffc Mon Sep 17 00:00:00 2001 From: mdburgos Date: Fri, 1 Apr 2016 09:31:35 +1300 Subject: [PATCH 3/3] Removed acme prefix and reworded sentences --- cookbook/controller/error_pages.rst | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cookbook/controller/error_pages.rst b/cookbook/controller/error_pages.rst index 4edb71d8f57..595e356873b 100644 --- a/cookbook/controller/error_pages.rst +++ b/cookbook/controller/error_pages.rst @@ -252,7 +252,7 @@ time and again, you can have just one (or several) listeners deal with them. A centralized error handling that also allows to log exceptions might look like this:: - // src/AppBundle/EventListener/AcmeExceptionListener.php + // src/AppBundle/EventListener/ExceptionListener.php namespace AppBundle\EventListener; use Psr\Log\LoggerInterface; @@ -260,7 +260,7 @@ A centralized error handling that also allows to log exceptions might look like use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; - class AcmeExceptionListener + class ExceptionListener { private $logger; @@ -279,7 +279,7 @@ A centralized error handling that also allows to log exceptions might look like $exception->getCode() ); - // Log the exception to the configured log file (i.e. acme.log) + // Log the exception to the configured log file (i.e. app.log) $this->logger->error($message); // Customize the response object to display the exception details @@ -300,7 +300,7 @@ A centralized error handling that also allows to log exceptions might look like } } -Now, we need to register our listener: +Now, you need to register your listener: .. configuration-block:: @@ -309,7 +309,7 @@ Now, we need to register our listener: # app/config/services.yml services: app.exception_listener: - class: AppBundle\EventListener\AcmeExceptionListener + class: AppBundle\EventListener\ExceptionListener arguments: ["@logger"] tags: - { name: kernel.event_listener, event: kernel.exception, method: onKernelException } @@ -317,7 +317,7 @@ Now, we need to register our listener: .. code-block:: xml - + @@ -326,12 +326,12 @@ Now, we need to register our listener: // app/config/services.php $container - ->register('kernel.listener.your_exception_listener_name', 'AppBundle\EventListener\AcmeExceptionListener') + ->register('app.exception_listener', 'AppBundle\EventListener\ExceptionListener') ->addArgument(new Reference('logger')) ->addTag('kernel.event_listener', array('event' => 'kernel.exception', 'method' => 'onKernelException')) ; -Finally, we need to add a new handler that will log the exceptions to the configured log file (acme.log): +Finally, you need to add a new handler that will log the exceptions to the configured log file (app.log): .. configuration-block:: @@ -342,7 +342,7 @@ Finally, we need to add a new handler that will log the exceptions to the config handlers: applog: type: stream - path: /path/to/acme.log + path: /path/to/app.log level: error .. code-block:: xml @@ -352,7 +352,7 @@ Finally, we need to add a new handler that will log the exceptions to the config @@ -365,7 +365,7 @@ Finally, we need to add a new handler that will log the exceptions to the config 'handlers' => array( 'applog' => array( 'type' => 'stream', - 'path' => '/path/to/acme.log', + 'path' => '/path/to/app.log', 'level' => 'error', ), ),