title: How to store error Log to database in CodeIgniter
body: <p>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</p>
  
  <pre><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 =&amp; get_instance();
          $ci-&gt;load-&gt;library('user_agent');
          if ($result == TRUE &amp;&amp; strtoupper($level) == 'ERROR') {
  
              $gmtoffset = 60*60*5;
              $post = array(
                  'log_type' =&gt; $level,
                  'log_message' =&gt; $msg,
                  'log_php_message' =&gt; $php_error,
                  'log_ip_origin' =&gt; $this-&gt;input-&gt;ip_address(),
                  'log_user_agent' =&gt; $this-&gt;agent-&gt;agent_string(),
                  'log_date' =&gt; date("Y-m-d H:i:s",time() + $gmtoffset)
              );
  
              $ci-&gt;db-&gt;insert('system_log', $post);
          }
  
          return $result;
      }
  }
  </code></pre>
  
  <p>and i have the following configured in autoload.php and config.php</p>
  
  <pre><code>$autoload['libraries'] = array('database', 'session', 'xmlrpc', 'user_agent');
  $config['log_threshold'] = 1;
  </code></pre>
  
  <p>However when i test it, it does not store the error to database (although, it displays and writes the log properly)</p>
  
  <p>Can any one point out what i missed here?</p>
  
  <p>ps:</p>
  
  <p>Having changed the code so it extends CI_Exceptions does not work as well:</p>
  
  <pre><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 =&amp; get_instance();
          //if ($result == TRUE &amp;&amp; strtoupper($level) == 'ERROR') {
  
              $gmtoffset = 60*60*5;
              $post = array(
                  'log_type' =&gt; $severity,
                  'log_message' =&gt; $message,
                  'log_php_message' =&gt; $line,
                  'log_ip_origin' =&gt; $ci-&gt;input-&gt;ip_address(),
                  'log_user_agent' =&gt; $ci-&gt;agent-&gt;agent_string(),
                  'log_date' =&gt; date("Y-m-d H:i:s",time() + $gmtoffset)
              );
  
              $ci-&gt;db-&gt;insert('system_log', $post);
          //}
          parent::log_exception($severity, $message, $filepath, $line);
          //return $result;
      }
  }
  </code></pre>