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?
根据定义,发送邮件是一项昂贵的操作(因为它很可能需要联系SMTP服务器),因此当您对程序进行概要分析时,与在程序的其他行中花费的时间相比,在error_log中花费的时间将是巨大的。
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.