使用Monolog在Symfony2中记录PHP致命错误

时间:2014-03-12 18:02:40

标签: symfony monolog

我需要一种方法来捕获PHP致命错误(还有通知和警告)并使用Monolog记录它们。

我发现Monolog 1.6+有ErrorHandler :: register()方法,但我无法弄清楚如何在Symfony2(生产)应用程序中使用它,以及如何在config.yml中正确配置它。

2 个答案:

答案 0 :(得分:11)

感谢@jenechka,他指出了我正确的方向,我想我找到了一个解决方案:

services.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);
    }

} 

通过这种方式,我可以将每个错误记录到monolog。我不认为这个解决方案可以被视为最佳实践&#34;所以我仍然在寻找更好的解决方案。

答案 1 :(得分:1)

按照http://symfony.com/doc/current/cookbook/service_container/event_listener.html

所述创建异常监听器

并在此处http://symfony.com/doc/current/reference/dic_tags.html#monolog-logger了解如何将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());
    }
}

更新:我改进了异常监听器,现在它处理致命异常。经过测试(在开发环境中)!