dongwei3120 2011-03-19 23:50
浏览 43
已采纳

挂钩错误处理周期

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?

  • 写回答

2条回答 默认 最新

  • dongpu2476 2011-03-20 15:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端