我需要一种方法来捕获 PHP 致命错误(还有通知和警告)并使用 Monolog 记录它们。
我发现 Monolog 1.6+ 有 ErrorHandler::register() 方法,但我不知道如何在 Symfony2(生产)应用程序中使用它,以及如何在 config.yml 中正确配置它。
感谢@jenechka,他为我指明了正确的方向,我想我找到了解决方案:
服务.yml:
vir.exception.listener:
class: %vir.exception.listener.class%
arguments: ["@logger"]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
错误处理程序:
<?php
namespace Mitecube\VoglioilruoloBundle\Listener;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Psr\Log\LoggerInterface;
class VoglioilruoloErrorHandler extends ErrorHandler {
private $logger;
private $prevErrorHandler;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
$this->prevErrorHandler = set_error_handler(array($this, 'handle'));
register_shutdown_function(array($this, 'handleFatal'));
}
public function onKernelRequest(GetResponseEvent $event)
{
}
public function handle($level, $message, $file = 'unknown', $line = 0, $context = array())
{
$this->logger->error($level . ": " . $message . " - in file " . $file . " - at line " . $line);
return parent::handle($level, $message, $file, $line, $context);
}
}
通过这种方式,我能够将每个错误记录到独白中。我不认为这个解决方案可以被认为是“最佳实践”,所以我仍在寻找更好的解决方案。
如此处所述创建异常侦听器http://symfony.com/doc/current/cookbook/service_container/event_listener.html
并在这里查看http://symfony.com/doc/current/reference/dic_tags.html#monolog-logger如何将记录器作为参数发送给您的听众
服务示例配置:
# src/Acme/DemoBundle/Resources/config/services.yml
parameters:
# ...
services:
# ...
kernel.listener.your_listener_name:
class: Acme\DemoBundle\EventListener\AcmeExceptionListener
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 }
监听器示例:
// src/Acme/DemoBundle/EventListener/AcmeExceptionListener.php
namespace Acme\DemoBundle\EventListener;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
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());
}
}
更新:我改进了异常侦听器,现在它可以处理致命异常。经过测试(在开发环境中)!