sunwukung
sunwukung

Reputation: 2815

PHP error_log performance issues

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?

Upvotes: 2

Views: 1271

Answers (3)

sunwukung
sunwukung

Reputation: 2815

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.

Upvotes: 1

pishfig
pishfig

Reputation: 41

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

Upvotes: 4

fvu
fvu

Reputation: 32953

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.

Upvotes: 4

Related Questions