2011-04-23 74 views
0

我想重写CI_Log类来改善日志行。 我想记录调用类的名称和方法。 例子:如何记录通话类信息?

DEBUG - 2011-04-23 09:21:29 - MY_Class - method --> Router Class Initialized 

我试图重写write_log mehod这样的:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class MY_Log extends CI_Log { 

    public function write_log($level = 'error', $msg, $php_error = FALSE) 
    { 
     [...] 

     $message .= 
      $level . 
      (($level == 'INFO') ? ' - ' : ' - ') . 
      $this->router->fetch_class() . 
      ' - ' . 
      $this->router->fetch_method() . 
      ' - ' . 
      date($this->_date_fmt). ' --> ' . 
      $msg . 
      "\n"; 

     [...] 
    } 

} 

但它不工作,$this->router不能访问。 你能帮我吗?

+0

当然,'$ this-> router'只能用于控制器,所以这不是一个好主意。我希望这为模型等工作... – 2011-04-23 08:30:29

回答

0

好吧,我想我得到了它。这里是我的代码:

[...] 

    $message .= $level.(($level == 'INFO') ? ' - ' : ' - ') . date($this->_date_fmt); 

    $stack = debug_backtrace(); 
    // We are searching for the 2nd element of the $stack array beacause : 
    // - $stack[0] is always taken by JG_Log->write_log() 
    // - $stack[1] is always taken by log_message() 
    if (isset($stack[2]['class'])) { 
     $message .= ' - ' . $stack[2]['class'] . ' - ' . $stack[2]['function']; 
    } 

    $message .= ' --> '.$msg."\n"; 

    [...] 

我希望能帮助别人! :-)

1

您可以在某处你的方法使用:

 
//$RTR is available from system/core/CodeIgniter.php 
global $RTR; 
$router_class = $RTR->fetch_class(); 
$rotuer_method = $RTR->fetch_method(); 
+0

感谢您的答案。因为Log类是在Router类之前初始化的,所以我必须使用下面的语法: '$ router_class =''; \t \t $ router_method =''; \t \t // $ RTR可从system/core/CodeIgniter.php获得 \t \t global $ RTR; (isset($ RTR)){ \t \t \t $ router_class = $ RTR-> fetch_class(); \t \t \t $ router_method = $ RTR-> fetch_method(); \t} 但是,这只适用于当前的控制器... – 2011-04-26 07:52:38

+0

我解释:我想,当控制器/模型/任何人调用'log_message('调试','blabla');'它会生成下面一行'DEBUG - 2011-04-23 09:21:29 - 类 - 方法 - >路由器类初始化' – 2011-04-26 07:57:04