In CodeIgniter, How Can I Have PHP Error Messages Emailed to Me? In CodeIgniter, How Can I Have PHP Error Messages Emailed to Me? codeigniter codeigniter

In CodeIgniter, How Can I Have PHP Error Messages Emailed to Me?


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


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)