Zend Framework - ZFDebug - Log - Log Custom Errors

2019-02-26 11:23发布

问题:

When using ZFDebug, is it possible to add custom messages to the 'Log' tab?

So you could use something like:

$this->log('Error: Couldn't find the user');

Has anyone managed to achieve this?

回答1:

I have never used ZFDebug before and wasn't aware of it. Your post piqued my interest, so I installed it and have been trying to achieve what you want to do. I will probably add it to my dev toolbox as I use ZF a lot.

You can achieve what you want by using the mark() method of ZFDebug_Controller_Plugin_Debug_Plugin_Log which takes two arguments. The first is the message you want to send and the second is a boolean which, when set to true (default is false), will send your message to the 'log' tab.

The following code worked for me:-

$debug = Zend_Controller_Front::getInstance()
             ->getPlugin('ZFDebug_Controller_Plugin_Debug');
$logger = $debug->getPlugin('log');
$logger->mark('Logging a message now', true);

Or to use your example (with the syntax error fixed :) )

$logger->mark("Error: Couldn't find the user", true);

As you can see this produced the desired output:-

Not quite as simple as you wanted, I know, but it's close and you could always wrap it in a function.