如何记录调用类信息?

时间:2011-04-23 08:28:50

标签: php codeigniter logging

我想覆盖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无法访问。 你能帮帮我吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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";

    [...]

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