views:

224

answers:

3

Hi, I've been trying to write an error handling class that I can use on sites, that will email me in the event of an error. Problem is, when I profile the application, it's choking on the error_log function. Here's my code (omitting the class:

class ErrorHandler
{
private static $instance;
private static $mail;
private function __clone(){}

private function __construct()
    {
    error_reporting( E_ALL | E_STRICT );

    if(!defined('ENV')){
        if($_SERVER['SERVER_ADDR']=='127.0.0.1' || $_SERVER['SERVER_NAME']=='localhost')
            {
            #echo"local environment<br>";
            DEFINE('ENV','LOCAL');
            ini_set('display_errors', 1);
            }
        else
            {
            #echo"live environment";
            DEFINE('ENV','LIVE');
            ini_set('display_errors', 0);
            }
        }
    }
public function setErrorConfig($error_level,$mail='',$mode='production')
    {
    error_reporting($error_level);
    switch($mode)
        {
        case 'development':
        ini_set('display_errors', '1');
        break;

        case 'production':
        ini_set('display_errors', '0');
        if($mail != ''){
            self::$mail = $mail;
            set_error_handler(array('ErrorHandler', 'handleError'));
            }
        break;

        default:
        ini_set('display_errors', '0');
        error_reporting( E_ERROR );
        break;
        }
    }

public function handleError($e_num,$e_msg,$e_file,$e_line,$e_vars)
    {
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: DC_Research Site' . "\r\n";

    $msg = '';
    $msg .= '<html><head></head><body>';
    $msg .= '<STYLE>h2{font-family:verdana;}</STYLE>';
    $msg .= '<h2>Error Description:</h2>';
    $msg .= '<h2>Script:</h2><p>'.$e_file.'</p>';
    $msg .= '<h2>Line:</h2><p>'.$e_line.'</p>';
    $msg .= '<h2>Message:</h2><p>'.$e_msg.'</p>';
    $msg .= '<h2>Variables:</h2><p>'.$e_vars.'</p>';
    $msg .= '</html></body>';

    #mail(self::$mail,'Error Report',$msg,$headers);
    error_log($msg,1,self::$mail,$headers);
    }
}

Can you help me figure out what's killing it?

+3  A: 

By definition sending a mail is an expensive operation (because it has to contact an SMTP server quite probably), so when you profile your program the time spent in error_log will be enormous compared to time spent in other lines of your program.

fvu
the program really chugs though - I've not noticed it getting so hammered using a regular mail script
sunwukung
can't answer that without more details from your setup, rewriting the thing to buffer errors in a temp file (i'd prefer a file to a database as pishfig suggested because the db can break as well and you may want to be notified of that kind of trouble) is probably less trouble than figuring out what's going on.
fvu
+1  A: 

You could store the error information in the database and then have a cron script email you the contents. I think saving to the DB will be quicker for the user than sending an email

pishfig
that's an interesting angle, thanks - I'll give that a whirl
sunwukung
A: 

I finally solved this issue - my bad. Trying to set the error handler to log to mail only causes the script to chug on a local setup - since it can't find a mail server (I assume). Wrapping the method call in a conditional that detects the location stops the problem.

sunwukung