Emailing errors generated by the log_message() function |
[eluser]alive[/eluser]
I sprinkled the log_message() function willy-nilly throughout my application with the intent that when I moved to production I could extend a core library so that log_message() emails me every "error" it's fed. Code: class MY_Log extends CI_Log I tried extending the Log class (which log_message() uses), but found that the base class is not loaded until after the Log class (as described in this thread). This is trouble because I cannot use the CI object (i.e. to load the email class) because it doesn't exist yet. I get the fatal error Quote:Fatal error: Call to undefined function get_instance() in /[site info]/system/application/libraries/MY_Log.php on line 16 I'm sure I'd be able to concoct some hack wherein a hook passes the CI object to the Log class and things more or less work (or just use native php email functions), but I'm wondering if anyone more knowledgeable can offer a better solution. Your help is greatly appreciated.
[eluser]bientek[/eluser]
The problem with sending an email on every error is that a warning message generated by code deep inside a nested loop is going to flood your inbox. Email providers might ban you, thinking you are misusing their service. Here is the solution I ended up using to periodically email errors and save them to the database. In my MY_Log, code temporarily saves the error info in a flat file for a cron consumer script to process for DB storage and email. Each minute, the cron task reads and deletes the file by calling Code: consume_errors() Update 5/15: attempted fix of problem with serialization of certain error messages by calling trim() Code: <?php
[eluser]bientek[/eluser]
This code actually produces an error during the unserialize process when something is echoed before the view is displayed. The offset is always near the end of the bytes that are being unserialized. Not sure what's wrong yet. Quote:A PHP Error was encountered
[eluser]bientek[/eluser]
The serialization failure was solved with this change: Code: if (file_exists(self::$error_filename)) Also, I changed the file locking to non-blocking since this caused a server crash when many of the "headers already sent" warnings were generated. The locking still prevents corruption from multiple writers, but it won't actually allow additional errors to be logged at all while one is currently being logged. This is a tradeoff: Code: if (flock($lock_file_handle, LOCK_EX | LOCK_NB) === FALSE) { Here is the updated code: Code: <?php |
Welcome Guest, Not a member yet? Register Sign In |