Better to implement a global instance of Zend_Log?

Is there a better way to have a globally accessible Zend_Log object?

In Bootstrap.php, I do:

protected function _initLogging()
{
    $logger = new Zend_Log();
    $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../app.log');
    $logger->addWriter($writer);
    Zend_Registry::set('log', $logger);
}

Then in the whole application I will use:

Zend_Registry::get('log')->debug('hello world');

This is not terrible, I think. It just looks verbose.

+3
source share
3 answers

Quote from a newsgroup entry :

class My_Helper_Logger extends Zend_Controller_Action_Helper_Abstract
{
  function init()
  {
    $this->getActionController()->logger = $this->getActionController()
                                                  ->getInvokeArg('bootstrap')
                                                    ->getResource('logger');
  }
}

to use the helper, all you have to do is insert init () Michael function an example of this line:

Zend_Controller_Action_HelperBroker::addHelper(new My_Helper_Logger());

and in every action of your controllers you can now access the logger as $ this-> logger

There are a number of other suggestions, so check the link.

+5
source

well, you can wrap it in a "manager class":

class My_Log_Manager
{
  static $_instance = null;
  protected $_logInstance = null;

  public static function getInstance(){}
  public function getLogInstance(){}
  public static function log($level, $msg)
  {
     self::getInstance()->_logInstance->$level($msg);
  }
}

, - , __call - . jsut , My_Log_Manager::<methodName>().

+1

You can try skipping addWriter()to delete one line:

$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../app.log');  
$logger = new Zend_Log($writer);  
Zend_Registry::set('log', $logger);
+1
source

Source: https://habr.com/ru/post/1728350/


All Articles