CodeIgniter Forums
Extending the CI_Log and CI_Exceptions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Extending the CI_Log and CI_Exceptions (/showthread.php?tid=58030)



Extending the CI_Log and CI_Exceptions - El Forum - 05-07-2013

[eluser]Unknown[/eluser]
I would like to store system error log to a database by extending the existing CI_Log class. So far this is what i have done

Code:
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;
        }
    }

and i have the following configured in autoload.php and config.php

$autoload['libraries'] = array('database', 'session', 'xmlrpc', 'user_agent');
$config['log_threshold'] = 1;

However when i test it, it does not store the error to database (although, it displays and writes the log properly)

Can any one point out what i missed here?

ps:

Having changed the code so it extends CI_Exceptions does not work as well:

Code:
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;
        }
    }

and yes i also post this issue at stackoverflow here http://stackoverflow.com/questions/16413781/how-to-store-error-log-to-database-in-codeigniter?noredirect=1#comment23534791_16413781