codeigniter - 如何在CodeIgniter中将错误日志存储到数据库

标签 codeigniter error-logging

我想通过扩展现有的 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;
    }
}

最佳答案

我相信我不久前尝试过这一点,并得出结论,不应引用 MY_Log 中的 CI 实例,因为它尚不可用(或尚未总是构建)。

关于codeigniter - 如何在CodeIgniter中将错误日志存储到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16413781/

相关文章:

php - 如何在 CodeIgniter 中获取关系数据

codeigniter - 如何扩展自己的库?

sails.js - 我正在使用 sailsjs,我想将 winston-sentry 与它集成。我在向 Sentry 添加元数据方面运气不佳。我的代码如下 :

python-2.7 - Python 2.7 默认情况下是否将错误写入日志文件?

php - 未写入 Apache 错误日志的错误

logging - 去哪里记录错误

php - HTML表单输入到mysql TIME

php - 错误号 : 1048 Column 'btc' cannot be null

c# - 如何使用 ELMAH 手动记录错误

php - 基于 PHP codeigniter 的网站上的搜索选项