加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 更好地实现全局Zend_Log实例?

发布时间:2020-12-13 21:35:58 所属栏目:PHP教程 来源:网络整理
导读:有没有更好的方法来拥有一个全局可访问的Zend_Log对象? 在Bootstrap.php我正在做: protected function _initLogging(){ $logger = new Zend_Log(); $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../app.log'); $logger-addWriter($writer);
有没有更好的方法来拥有一个全局可访问的Zend_Log对象?

在Bootstrap.php我正在做:

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

然后在整个应用程序中我将使用:

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

我想这并不可怕.它看起来有点冗长.

解决方法

引用此 newsgroup entry:

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

to apply the helper all you have to do is to put in the init()
function of Michael’s example this line:

Zend_Controller_Action_HelperBroker::addHelper(new My_Helper_Logger());

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

还有许多其他建议,请查看链接.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读