用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3635|回复: 4
收起左侧

Log类中无法使用&get_instance()函数

[复制链接]
发表于 2012-8-25 16:19:44 | 显示全部楼层 |阅读模式
本帖最后由 fltn03 于 2012-8-25 16:23 编辑

前不久对CI的Log类进行了扩展,增加了一种日志类别——RECOR(即record),其级别介于Error与Debug之间。本以为没有问题,但今天在使用的时候发现了一个问题,如题所述:Log类中无法使用&get_instance()函数。
贴上我以前的代码:
1、config.php中信息:
PHP复制代码
 
/*
|--------------------------------------------------------------------------
| Error Logging Threshold
|--------------------------------------------------------------------------
|        0 = Disables logging, Error logging TURNED OFF
|        1 = Error Messages (including PHP errors)
|        2 = Record Message
|        3 = Debug Messages
|        4 = Informational Messages
|        5 = All Messages
|
*/

$config['log_threshold'] = 3;
 
/*
|--------------------------------------------------------------------------
| 是否开启锁文件机制
|--------------------------------------------------------------------------
|
| 在进行文件的读写操作时需要用到文件的锁机制,但是在一些系统中使用锁机制会影响到
| 网站的整体性能,所以特地增加了是否需要开启锁机制的功能。
|        FALSE:不开启;TRUE:开启
|
*/

$config['flock_enable'] = FALSE;
 
复制代码

2、MY_Log.php文件:
PHP复制代码
 
class MY_Log extends CI_log {
       
        /**
         * 日志级别
         */

        protected $_levels = array(        'ERROR'         => '1',
                                                    'RECOR'         => '2',
                                                    'DEBUG'         => '3',
                                                    'INFRO'         => '4',
                                                    'ALL'             => '5');
       
        /**
         * Write Log File
         * 扩展原生方法
         * 去掉了写文件时的锁文件机制,因为锁机制会影响到系统的整体性能。
         *
         * Generally this function will be called using the global log_message() function
         *
         * @param        string        the error level
         * @param        string        the error message
         * @param        bool        whether the error is a native PHP error
         * @return        bool
         */

        public function write_log($level = 'error', $msg, $php_error = FALSE)
        {
                if ($this->_enabled === FALSE)
                {
                        return FALSE;
                }
 
                $level = strtoupper($level);
 
                if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
                {
                        return FALSE;
                }
 
                $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
                $message  = '';
 
                if ( ! file_exists($filepath))
                {
                        $message .= "<"."?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
                }
 
                if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
                {
                        return FALSE;
                }
               
                $CI = & get_instance();//该处代码当$config['log_threshold']>2时就报错
                //获取客户端IP地址
                $ip = $CI->input->ip_address();
                $ip_string = $ip . str_repeat('-',(15 - strlen($ip)) );
                unset($CI);
                $message .= $level . ' ' . (($level == 'INFO') ? ' -' : '-') . ' ' . date($this->_date_fmt)
                                        . '---' . $ip_string . ' --> ' . $msg . "\n";
               
                //判断是否需要开启文件锁机制
                $config = & get_config();
                //判断config.php配置文件中是否有该配置
                if(! isset($config['flock_enable']))
                {
                        $config['flock_enable'] = TRUE;
                }
               
                if($config['flock_enable'] === FALSE)
                {
                        fwrite($fp, $message);
                }
                else
                {
                        flock($fp, LOCK_EX);
                        fwrite($fp, $message);
                        flock($fp, LOCK_UN);
                        fclose($fp);
                }
 
                @chmod($filepath, FILE_WRITE_MODE);
                return TRUE;
        }
       
        // --------------------------------------------------------------------        
}
 
/* End of file MY_Log.php */
/* Location: ./application/libraries/MY_Log.php */
 
复制代码

说明:为了在日志中记录下客户端的IP地址,所有使用了input类中的ip_adress()方法获取;便使用了
$CI = & get_instance();

但现在问题出现了:
当$config['log_threshold']<=2时,log_message()函数能正常运行;
但是当$config['log_threshold']>2时,log_message()函数就报错:
Fatal error: Class 'CI_Controller' not found in D:\xampp\htdocs\CI\system\core\CodeIgniter.php on line 233

经过查找原因后发现:
在Log类中添加”$CI = &  get_instance();“一行代码就会报错。
按照CI2.1手册的介绍,如果在library类中使用CodeIgniter对象,只需要“$CI =& get_instance();”即可。
的确,在其他的library中操作是没有任何问题,但是在log类中却存在问题!

如果以上分析有问题,欢迎指正!
发表于 2012-8-26 01:06:14 | 显示全部楼层
$ip = $CI->input->ip_address();

=》

$input = &load_class('Input');
$ip = $input->ip_address();
 楼主| 发表于 2012-8-26 13:49:07 | 显示全部楼层
太尉天上飞 发表于 2012-8-26 01:06
$ip = $CI->input->ip_address();

=》

非常感谢你提供的那种方法!
但是Log类中还是没法使用“&get_instance();”,这个是重点!
发表于 2012-8-26 14:35:42 | 显示全部楼层
load_class() 的时候 若将debug等信息记录日志,那个时候get_instance还未定义好好看看源码吧
 楼主| 发表于 2012-8-26 16:16:26 | 显示全部楼层
太尉天上飞 发表于 2012-8-26 14:35
load_class() 的时候 若将debug等信息记录日志,那个时候get_instance还未定义好好看看源码吧 ...

一语惊醒梦中人!
感谢你的提醒!

本版积分规则