CodeIgniter Forums
Writing errors to database / emailing them - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Writing errors to database / emailing them (/thread-21749.html)



Writing errors to database / emailing them - El Forum - 08-19-2009

[eluser]LinkFox[/eluser]
THis is driving me mad now.

Has anyone managed to extend CI's error handling to write all errors to the database and email those bad boys to someone?


Writing errors to database / emailing them - El Forum - 08-19-2009

[eluser]jcavard[/eluser]
You'll have to paste more information if you expect an answer...
What is not working? Do you have code so far%


Writing errors to database / emailing them - El Forum - 08-19-2009

[eluser]LinkFox[/eluser]
Well,

I would like to send any errors that occur in my application to a database table called errors which will allow me to monitor my application errors using reports.

I was wondering if anyone had extended or knew of an extension to allow this using CI's existing error handling functions.

Thanks Smile


Writing errors to database / emailing them - El Forum - 08-21-2009

[eluser]itibook[/eluser]
I am new here so there might be something easier, but I think you can easily just use a controller to write to the "log table" where you can record pretty much anything...


Writing errors to database / emailing them - El Forum - 08-21-2009

[eluser]davidbehler[/eluser]
Google solves --> Post on StackOverFlow.com:

Make a file MY_Exceptions.php and place it in /application/libraries/:
Code:
class MY_Exceptions extends CI_Exceptions {

    function My_Exceptions()
    {
        parent::CI_Exceptions();
    }

    function log_exception($severity, $message, $filepath, $line)

    {  

        $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];

        log_message('error', 'Severity: '.$severity.'  --> '.$message. ' '.$filepath.' '.$line, TRUE);

        $this->load->library('email');
        $this->email->from('[email protected]', 'Your Name');
        $this->email->to('[email protected]');
        $this->email->cc('[email protected]');
        $this->email->bcc('[email protected]');

        $this->email->subject('error');
        $this->email->message('Severity: '.$severity.'  --> '.$message. ' '.$filepath.' '.$line);

        $this->email->send();
    }

}

Untested but might work Smile


Writing errors to database / emailing them - El Forum - 11-06-2009

[eluser]Unknown[/eluser]
add a table in your database as follows:
Code:
SET FOREIGN_KEY_CHECKS=0;
    -- ----------------------------
    -- Table structure for errores
    -- ----------------------------
    DROP TABLE IF EXISTS `errores`;
    CREATE TABLE `errores` (
      `id` int(11) NOT NULL auto_increment,
      `Severity` varchar(50) default NULL,
      `message` text,
      `filepath` varchar(250) default NULL,
      `line` varchar(250) default NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/system/libraries/Exceptions
modify the function log_exception($severity, $message, $filepath, $line) as this:
add the following line:
Code:
$this->log_execption_db($severity, $message, $filepath, $line);

add the function log_execption_db to the system library
Code:
function log_execption_db($severity, $message, $filepath, $line){
        $this->ci =& get_instance();
        $this->ci->load->database();
        
            $data = array(
               'Severity' => $severity ,
               'message' => $message ,
               'filepath' => $filepath,
               'line' => $line
            );

        $this->ci->db->insert('errores', $data);
        
    }

And "ya esta", all the errors will be stored in database too. Also we could create a new confi item in our application to determines if we would like to stored the errors in DB, in file or in both.