EllisLab text mark
Advanced Search
     
DB Error Logging tied with CI Error Logging
Posted: 23 September 2008 06:53 PM
Avatar
Joined: 2008-08-29
344 posts

So to better manage errors that popup throughout the system.

Main use would be for reporting errors while a user is doing things of course.

This would make it easier for an inexperienced person who runs a website built upon CI to manage bugs, tell a admin, IT guy or whoever what is exactly wrong.

However I want to keep the original functionality of the CI error exception handler already built in just add the DB logging.

However i noticed where it logs the message (System/codeigniter/common.php) the database has yet to be setup yet.

So my delema here is where would I overwrite “function log_message” with my log_message Which contains a few more lines to also log it to the database.


A few things i looked up seemed like I could use some hooks, or creating a new library. However its just 2 lines of code to have it added to the database. So without rewriting a lot of it i want to just add that.


Any Ideas? Im also a newbie when it comes to CI im slowy picking it up but its coming so all the help would be great!

 Signature 

421 Entertainment :: YAAS - Yet Another Authentication System :: Error Logger :: DB Configs

 
Posted: 07 February 2009 05:56 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-17
426 posts

did u end up having luck doing this? im in the same boat

 Signature 

aka trs21219
CodeSanity | Github | LinkedIn | Facebook | Twitter | Last.fm

 
Posted: 08 February 2009 04:42 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-29
344 posts

I have not yet however I am still planning on doing it! I’ll post a solution as soon as I find one…

I have a feeling I will be overwriting part of the exception class to be able to do this and tie into he error logging.

 Signature 

421 Entertainment :: YAAS - Yet Another Authentication System :: Error Logger :: DB Configs

 
Posted: 08 February 2009 04:44 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-17
426 posts

ya i have a feeling it will include hacking the core also. what about a hook? ive not used them yet but is it possible to get them to work with this?

 Signature 

aka trs21219
CodeSanity | Github | LinkedIn | Facebook | Twitter | Last.fm

 
Posted: 08 February 2009 04:51 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-29
344 posts

Possible however I was thinking more about extending the exceptions class not hackn the core

 Signature 

421 Entertainment :: YAAS - Yet Another Authentication System :: Error Logger :: DB Configs

 
Posted: 20 February 2009 01:27 PM   [ # 5 ]   [ Rating: 0 ]
Joined: 2008-12-09
10 posts

Hi guys,

I ‘m currently asking myself about the reverse issue. I want to log DB errors through CI Exceptions to get them logged everytime, even with $db[‘mydbconfigset’][‘db_debug’] = FALSE;
in the configuration file.

I’ve already extended the CI_Exceptions class to get reports on fatal errors via email. I like to keep notices logged but I think I’ll put them in a separate file or something like.

I tackled the DB_Driver problem to extend it without modifying core file, and on option I found was to leave the db_debug param on and handle real debugging and production logging by modifying the show_error. It’s a bit weird as a solution, but I’ll give it a try.


Here’s my quick& dirt extension to handle

<?php
/**
 * Custom error handling
 */

class MY_Exceptions extends CI_Exceptions {
  
private $ci;
  private 
$ignore_duplicates
  private 
$last_php_error_message;
  private 
$last_db_error_message;
  private 
$report_from_email;
  private 
$report_to_email;
  
  function 
MY_Exceptions() {
    parent
::CI_Exceptions();
    
$this->ignore_duplicates TRUE;
    
$this->last_php_error_message '';
    
$this->last_db_error_message '';
    
    
$this->ci = &get;_instance();
    
$this->ci->load->library('email');
    
$this->report_from_email $this->ci->config->item('error_report_to_email');
    
$this->report_to_email $this->ci->config->item('error_report_from_email');
  
}
  
  
function __construct() {
    $this
->MY_Exceptions();
  
}
  
  
function log_exception($severity$message$filepath$line{    
        $severity 
= ( ! isset($this->levels[$severity])) ? $severity $this->levels[$severity];
        
        
$complete_message 'Severity: '.$severity.'  --> '.$message' '.$filepath;
        
        
log_message('error'$complete_message .' Filename:'.$lineTRUE);
        
        if (
$this->ignore_duplicates && $this->last_php_error_message == $complete_message{
          
return;
        
}
        
        $subject 
'PHP error report';
        
$body $complete_message "\nOn line : " $line;
        
        
$this->report_email($subject$body);

        
//ignore line number for error duplication check
        
$this->last_php_error_message $complete_message;
    
}
    
    
/**
     * General Error Page
     *
     * This function takes an error message as input
     * (either as a string or an array) and displays
     * it using the specified template.
     *
     * @access    private
     * @param    string    the heading
     * @param    string    the message
     * @param    string    the template name
     * @return    string
     */
    
function show_error($heading$message$template 'error_general')
    
{
        
//report only db errors 
        
if ($template == 'error_db'{
          $email_message 
$heading "\n\n" implode("\n", ( ! is_array($message)) ? array($message) : $message);
          if (
$this->ignore_duplicates && $this->last_db_error_message == $email_message{
             
return parent::show_error($heading$message$template);
          
}
          
          $this
->report_email('DB error report'$email_message);
          
$this->last_db_error_message == $email_message
        
}
        
return parent::show_error($heading$message$template);
    
}
    
    
private function report_email($subject$message{
      $this
->ci->email->from($this->report_from_email'CI errors');
      
$this->ci->email->to($this->report_to_email); 
      
$this->ci->email->subject($subject);
      
$this->ci->email->message($message); 
      return 
$this->ci->email->send();
    
}
}
?> 

I need to add some other stuff to disable email reporting while in developement mode and hide default CI db error with a nice excuse page while in production.

tdktank59, I think you can record php errors in a DB table the same way

Best regards,
Ben

 
Posted: 20 February 2009 10:44 PM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-29
344 posts

Looks like I can just have to do some little modifications.

This was basically want I was planning on doing to some degree just havn’t had the time to get arround to it yet.

 Signature 

421 Entertainment :: YAAS - Yet Another Authentication System :: Error Logger :: DB Configs

 
Posted: 23 February 2009 06:48 AM   [ # 7 ]   [ Rating: 0 ]
Joined: 2008-12-09
10 posts

My code is having a problem dealing with 404 page errors : when this happens the CI_Base seems not loaded and the call to get_instance() in constructor fails.

I think it can be handled with a hook to force loading of this function/class before routing.

I’m in a hurge for the project I’m working on so I’ll fix that later

 
Posted: 23 February 2009 07:46 AM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-25
4956 posts

Obviously, the ability to write the log to a file is the most basic ability. It works like that so that your able to have a log, even if you’re database is broken.

I’m interested to know why you want to use your database for logging. If you absolutely must put your logs into a database, I would suggest running a cronjob that will do it periodically.

 Signature 

http://www.phptherightway.com

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.

 
Posted: 17 March 2009 10:36 AM   [ # 9 ]   [ Rating: 0 ]
Joined: 2008-12-09
10 posts

Hi,

I fixed my code so it works, I deem it was due to the CI_Exception class instanciation before get_instance method is available.

I moved this form constructor
$this->ci = &get;_instance();
$this->ci->load->library(‘email’);

at the beginning of report_email private func

ben