所以我想开始登录我的Zend Framework应用程序.我想在我的引导程序中的某处注册记录器,以便我可以从任何控制器操作轻松访问它.我认为这应该是以前做过的简单事情,但我该怎么做呢?
文档显示了这样的内容,但是每次我想记录某些东西时我都不想创建一个新的Zend_Log:
$writer = new Zend_Log_Writer_Stream('/path/to/my/log/file'); $logger = new Zend_Log($writer); $logger->log('Informational message', Zend_Log::INFO);
这就是我提出的.感谢关于Zend_Registry的提醒!
// in /application/Bootstrap.php protected function _initLogger() { $writer = new Zend_Log_Writer_Stream('php://output'); $logger = new Zend_Log($writer); Zend_Registry::set('logger', $logger); } // in controller actions $logger = Zend_Registry::get('logger'); $logger->log('message');
linead.. 7
最简单的方法是使用Zend_Registry存储日志
在你的引导程序中使用它
Zend_Registry::set('log', $log);
并使用它来获取该日志
Zend_Registry::get('log')
cwhisperer.. 5
我做了几乎相同但登录到数据库.这里我的代码放在Bootstrap中
public function _initLogger(){ $resource = $this->getPluginResource('db'); $dbAdapter = $resource->getDbAdapter(); $columnMapping = array( 'level' => 'priorityName', 'priority' => 'priority', 'message' => 'message', 'created' => 'timestamp', 'user_agent'=> 'user_agent', 'get_vars' => 'get_vars', 'post_vars' => 'post_vars', 'ip' => 'ip' ); $writerDb = new Zend_Log_Writer_Db($dbAdapter, 'tbl_history', $columnMapping); $logger = new Zend_Log($writerDb); $logger->setEventItem('datetime',date('Y-m-d H:i:s')); $logger->setEventItem('user_agent',$_SERVER['HTTP_USER_AGENT']); $logger->setEventItem('get_vars',print_r($_GET,true)); $logger->setEventItem('post_vars',print_r($_POST,true)); $logger->setEventItem('ip',$_SERVER['REMOTE_ADDR']); Zend_Registry::set('logger', $logger); }
并从以下任何地方访问它:
Zend_Registry::get('logger')->log('log emergency message', Zend_Log::EMERG); Zend_Registry::get('logger')->info('just logging some information');
希望这有助于;)
最简单的方法是使用Zend_Registry存储日志
在你的引导程序中使用它
Zend_Registry::set('log', $log);
并使用它来获取该日志
Zend_Registry::get('log')
我做了几乎相同但登录到数据库.这里我的代码放在Bootstrap中
public function _initLogger(){ $resource = $this->getPluginResource('db'); $dbAdapter = $resource->getDbAdapter(); $columnMapping = array( 'level' => 'priorityName', 'priority' => 'priority', 'message' => 'message', 'created' => 'timestamp', 'user_agent'=> 'user_agent', 'get_vars' => 'get_vars', 'post_vars' => 'post_vars', 'ip' => 'ip' ); $writerDb = new Zend_Log_Writer_Db($dbAdapter, 'tbl_history', $columnMapping); $logger = new Zend_Log($writerDb); $logger->setEventItem('datetime',date('Y-m-d H:i:s')); $logger->setEventItem('user_agent',$_SERVER['HTTP_USER_AGENT']); $logger->setEventItem('get_vars',print_r($_GET,true)); $logger->setEventItem('post_vars',print_r($_POST,true)); $logger->setEventItem('ip',$_SERVER['REMOTE_ADDR']); Zend_Registry::set('logger', $logger); }
并从以下任何地方访问它:
Zend_Registry::get('logger')->log('log emergency message', Zend_Log::EMERG); Zend_Registry::get('logger')->info('just logging some information');
希望这有助于;)