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 controlle
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());
}
}
);
}