Question

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 CodeIgniter?

Was it helpful?

Solution

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/ (Or in /application/core/ for CI 2)

class MY_Exceptions extends CI_Exceptions {

    function __construct()
    {
        parent::__construct();
    }

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

    {   
        if (ENVIRONMENT === 'production') {
            $ci =& get_instance();

            $ci->load->library('email');
            $ci->email->from('your@example.com', 'Your Name');
            $ci->email->to('someone@example.com');
            $ci->email->cc('another@another-example.com');
            $ci->email->bcc('them@their-example.com');
            $ci->email->subject('error');
            $ci->email->message('Severity: '.$severity.'  --> '.$message. ' '.$filepath.' '.$line);
            $ci->email->send();
        }


        parent::log_exception($severity, $message, $filepath, $line);
    }

}

OTHER TIPS

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.

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)

I'm just about to release an open source project that does this, and more. It collects errors, sends them to an issue tracker, detects duplicates, turns them into issues and emails staff.

Details are at https://sourceforge.net/news/?group_id=317819&id=293422 and the version 0.1.7 it mentions is due out in a couple of days.

The open source tracker is at http://elastik.sourceforge.net/

Any feedback welcome, Thanks

I set up an open source github project to implement this solution with configurable email parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top