views:

1881

answers:

4

I'd like to receive error logs via email. For example, if a Warning-level error message should occur, I'd like to get an email about it.

How can I get that working in CI ?

Thanks,

Ian

+10  A: 

You could extend the Exception core class to do it.

Might have to adjust the reference to CI's email class, not sure if you can instantiate it from a library like this. I don't use CI's email class myself, I've been using the Swift Mailer library. But this should get you on the right path.

Make a file MY_Exceptions.php and place it in /application/libraries/

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();
    }

}
Adam
Correction on the above, need to pluralize Exceptions.MY_Exceptions.phpclass MY_Exceptions extends CI_Exceptions { function My_Exceptions() { parent::CI_Exceptions(); }...(using CI version 1.7.1)
Jay
A: 

Oh, another option is to get a logrotation application that supports emailing digests. Not sure what platform you are on, but you could just have something monitor the error_log file and send you updates, might not be as neat and certainly you would be limited to only information in the error_log. (error_log is Apache, CI has a /logs/ folder in system, and IIS has the Windows Events)

Adam
A: 

I think this might be occur because of using editor provide on hosting server..

A: 

One thing that is left out of the solution is that you have to grab CodeIgniters super object to load and use the email library (or any of CodeIgniters other libraries and native functions).

$CI =& get_instance();

After you have done that you use $CI instead of $this to load the email library and set all of the parameters. For more information click here and look under the Utilizing CodeIgniter Resources within Your Library section.

Jon Terry