How to log ZF2 controller exceptions

2019-02-07 08:43发布

问题:

I'm registering a Zend\Log instance for exceptions, I will need all system errors emailed in the end, now it's just goes to a file. However, it doesn't work in controllers, the exception gets displayed in the view (or not, depending on display_exceptions). I found this bug, no one seems to care about it much. So I need a workaround. Is there a way to make controllers not to eat my exceptions?

'service_manager' => array(
    'factories' => array(
        'Logger' => function ($sm) use ($sRootDir)
            {
            $log = new Zend\Log\Logger();
            $writer = new Zend\Log\Writer\Stream($sRootDir . '/temp/license.log');
            $log->addWriter($writer);
            Zend\Log\Logger::registerErrorHandler($log);
            Zend\Log\Logger::registerExceptionHandler($log);
            return $log;
            },
    ),

回答1:

You can attach to the dispatch error event:

Module.php

public function onBootstrap(MvcEvent $e)
{
    $eventManager = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    /**
     * Log any Uncaught Errors
     */
    $sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
    $sm = $e->getApplication()->getServiceManager();
    $sharedManager->attach('Zend\Mvc\Application', 'dispatch.error',
         function($e) use ($sm) {
            if ($e->getParam('exception')){
                $sm->get('Logger')->crit($e->getParam('exception'));
            }
         }
    );
}

An example service config for a simple logger

'factories' => array(
    'Logger' => function($sm){
        $logger = new \Zend\Log\Logger;
        $writer = new \Zend\Log\Writer\Stream('./data/log/'.date('Y-m-d').'-error.log');
        $logger->addWriter($writer);
        return $logger;
    },
    // ...
);

You can also log all exceptions in the stack to get a better idea of what went wrong down the line, rather than only showing the last exception, which may not include much information.

public function onBootstrap(MvcEvent $e)
{
    $eventManager = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    /**
     * Log any Uncaught Exceptions, including all Exceptions in the stack
     */
    $sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
    $sm = $e->getApplication()->getServiceManager();
    $sharedManager->attach('Zend\Mvc\Application', 'dispatch.error',
        function($e) use ($sm) {
            if ($e->getParam('exception')){
                $ex = $e->getParam('exception');
                do {
                    $sm->get('Logger')->crit(
                        sprintf(
                           "%s:%d %s (%d) [%s]\n", 
                            $ex->getFile(), 
                            $ex->getLine(), 
                            $ex->getMessage(), 
                            $ex->getCode(), 
                            get_class($ex)
                        )
                    );
                }
                while($ex = $ex->getPrevious());
            }
        }
    );
}