我想通过扩展现有的 CI_Log 类将系统错误日志存储到数据库中。到目前为止,这就是我所做的
class MY_Logger extends CI_Log{
public function __construct(){
parent::__construct();
}
function write_log($level='error', $msg, $php_error = FALSE){
$result = parent::write_log($level, $msg, $php_error);
$ci =& get_instance();
$ci->load->library('user_agent');
if ($result == TRUE && strtoupper($level) == 'ERROR') {
$gmtoffset = 60*60*5;
$post = array(
'log_type' => $level,
'log_message' => $msg,
'log_php_message' => $php_error,
'log_ip_origin' => $this->input->ip_address(),
'log_user_agent' => $this->agent->agent_string(),
'log_date' => date("Y-m-d H:i:s",time() + $gmtoffset)
);
$ci->db->insert('system_log', $post);
}
return $result;
}
}
我在 autoload.php 和 config.php 中配置了以下内容
$autoload['libraries'] = array('database', 'session', 'xmlrpc', 'user_agent');
$config['log_threshold'] = 1;
但是,当我对其进行测试时,它不会将错误存储到数据库中(尽管它会正确显示和写入日志)
谁能指出我在这里错过了什么?
ps:
更改了代码以扩展 CI_Exceptions 也无法正常工作:
class MY_Exceptions extends CI_Exceptions{
function __construct(){
parent::__construct();
}
function log_exception($severity, $message, $filepath, $line){
//$result = parent::write_log($level, $msg, $php_error);
$ci =& get_instance();
//if ($result == TRUE && strtoupper($level) == 'ERROR') {
$gmtoffset = 60*60*5;
$post = array(
'log_type' => $severity,
'log_message' => $message,
'log_php_message' => $line,
'log_ip_origin' => $ci->input->ip_address(),
'log_user_agent' => $ci->agent->agent_string(),
'log_date' => date("Y-m-d H:i:s",time() + $gmtoffset)
);
$ci->db->insert('system_log', $post);
//}
parent::log_exception($severity, $message, $filepath, $line);
//return $result;
}
}