挂钩错误处理周期

发布于 2024-10-25 10:16:02 字数 1109 浏览 2 评论 0原文

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

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

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() 方法是一个扩展,只是设置一个受保护的标志。

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

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

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹微笑 2024-11-01 10:16:02

使用堆栈索引 101 注册您的插件。检查 routeShutdown 和 postDispatch 上响应对象中的异常。

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

要检查错误处理程序循环内是否抛出异常,您必须将dispatch()放在try-catch块中

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

背叛残局 2024-11-01 10:16:02

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

我编写了一个控制器插件,如下所示:

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 实例的数组。了解这一点后,我只是在记录器方法中添加了一个 foreach 循环,该循环将每个异常写入单独的日志。

现在几乎一切都按预期进行。目前我仍然记录了两个相同的异常,这不是我所期望的。我必须通过关于SO的另一个问题来调查这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文