06/02/2015, 11:52
|
| | Fecha de Ingreso: febrero-2011
Mensajes: 23
Antigüedad: 13 años, 9 meses Puntos: 0 | |
Respuesta: handle errores, warnings Basicamente use una clase que andaba circulando:
# src/Acme/DemoBundle/Resources/config/services.yml
parameters:
# ...
services:
# ...
kernel.listener.your_listener_name:
class: Acme\DemoBundle\EventListener\AcmeExceptionListene r
arguments: ["@logger"]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
- { name: monolog.logger, channel: tema } Listener example:
// src/Acme/DemoBundle/EventListener/AcmeExceptionListener.php
namespace Acme\DemoBundle\EventListener;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\HttpKernel\Event\GetResponseEven t;
use Symfony\Component\HttpKernel\Event\GetResponseForE xceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Psr\Log\LoggerInterface;
class ExceptionListener extends ExceptionHandler
{
private $logger;
private $prevExceptionHandler;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
// Set our handle method as fatal exception handler.
// It is required to extend Symfony\Component\Debug\ExceptionHandler
$this->prevExceptionHandler = set_exception_handler(array($this, 'handle'));
}
public function onKernelRequest(GetResponseEvent $event)
{
}
/**
* Handles non fatal exceptions (normal way).
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
// You get the exception object from the received event
$exception = $event->getException();
// Log exception.
$this->logger->error($exception->getMessage());
// ...
}
/**
* Overwrite ExceptionHandler method.
*/
public function handle(\Exception $exception) {
// Call our custom handler.
$this->onFatalErrorException($exception);
// Call exception handler that was overridden.
// Or try to call parent::handle($exception)
if (is_array($this->prevExceptionHandler) && $this->prevExceptionHandler[0] instanceof ExceptionHandler) {
$this->prevExceptionHandler[0]->handle($exception);
}
}
public function onFatalErrorException(\Exception $exception)
{
// Do anything you want...
$this->logger->error('Hey, I got it: '. $exception->getMessage());
}
} |