Hooking into the Error processing cycle

给你一囗甜甜゛ 提交于 2020-01-14 05:16:56

问题


I'm building a monitoring solution for logging PHP errors, uncaught exceptions and anything else the user wants to log to a database table. Kind of a replacement for the Monitoring solution in the commercial Zend Server.

I've written a Monitor class which extends Zend_Log and can handle all the mentioned cases. My aim is to reduce configuration to one place, which would be the Bootstrap. At the moment I'm initializing the monitor like this:

protected function _initMonitor()
{
    $config = Zend_Registry::get('config');
    $monitorDb = Zend_Db::factory($config->resources->db->adapter, $config->resources->db->params);

    $monitor = new Survey_Monitor(new Zend_Log_Writer_Db($monitorDb, 'logEntries'), $config->projectName);

    $monitor->registerErrorHandler()->logExceptions();
}

The registerErrorHandler() method enables php error logging to the DB, the logExceptions() method is an extension and just sets a protected flag.

In the ErrorController errorAction I add the following lines:

//use the monitor to log exceptions, if enabled
$monitor = Zend_Registry::get('monitor');

if (TRUE == $monitor->loggingExceptions)
{
    $monitor->log($errors->exception);
}

I would like to avoid adding code to the ErrorController though, I'd rather register a plugin dynamically. That would make integration into existing projects easier for the user.

Question: Can I register a controller plugin that uses the postDispatch hook and achieve the same effect? I don't understand what events trigger the errorAction, if there are multiple events at multiple stages of the circuit, would I need to use several hooks?


回答1:


Register your plugin with stack index 101. Check for exceptions in response object on routeShutdown and postDispatch.

$response = $this->getResponse();
if ($response->isException()) {
   $exceptions = $response->getException();
}

to check if exception was thrown inside error handler loop you must place dispatch() in a try-catch block




回答2:


The accepted answer by Xerkus got me on the right track. I would like to add some more information about my solution, though.

I wrote a Controller Plugin which looks like that:

class Survey_Controller_Plugin_MonitorExceptions extends Zend_Controller_Plugin_Abstract
{    
    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        $response = $this->getResponse();
        $monitor = Zend_Registry::get('monitor');

        if ($response->isException())
        {
            $monitor->log($response);
        }
    }
}

Note that you get an Array of Zend_Exception instances if you use $response->getException(). After I had understood that, I simply added a foreach loop to my logger method that writes each Exception to log separately.

Now almost everything works as expected. At the moment I still get two identical exceptions logged, which is not what I would expect. I'll have to look into that via another question on SO.



来源:https://stackoverflow.com/questions/5365770/hooking-into-the-error-processing-cycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!