进入错误处理循环 [英] Hooking into the Error processing cycle

查看:27
本文介绍了进入错误处理循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个监控解决方案,用于记录 PHP 错误、未捕获的异常以及用户想要记录到数据库表的任何其他内容.商业 Zend Server 中监控解决方案的替代品.

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.

我编写了一个 Monitor 类,它扩展了 Zend_Log 并且可以处理所有提到的情况.我的目标是将配置减少到一个地方,那就是 Bootstrap.目前我正在像这样初始化监视器:

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

registerErrorHandler() 方法将 php 错误记录到数据库中,logExceptions() 方法是一个扩展,只是设置了一个受保护的标志.

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

在 ErrorController errorAction 中,我添加以下几行:

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

不过,我想避免向 ErrorController 添加代码,我宁愿动态注册一个插件.这将使用户更容易集成到现有项目中.

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.

问题:我可以注册一个使用 postDispatch 钩子的控制器插件并达到同样的效果吗?我不明白是什么事件触发了errorAction,如果电路的多个阶段有多个事件,我是否需要使用多个钩子?

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?

推荐答案

Xerkus 接受的答案让我走上了正轨.不过,我想添加一些有关我的解决方案的更多信息.

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

请注意,如果您使用 $response->getException(),您将获得一个 Zend_Exception 实例数组.明白了这一点后,我简单地在我的 logger 方法中添加了一个 foreach 循环,该循环将每个 Exception 分别写入日志.

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.

现在几乎一切都按预期工作.目前我仍然记录了两个相同的异常,这不是我所期望的.我将不得不通过关于 SO 的另一个问题来研究这一点.

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.

这篇关于进入错误处理循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆