PHP error_log 性能问题

标签 php class error-handling

我一直在尝试编写一个可以在网站上使用的错误处理类,如果出现错误,它会向我发送电子邮件。问题是,当我分析应用程序时,它被 error_log 函数阻塞。这是我的代码(省略类:

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

你能帮我找出是什么杀死了它吗?

最佳答案

根据定义,发送邮件是一项昂贵的操作(因为它很可能必须联系 SMTP 服务器),因此当您分析程序时,与在程序的其他行中花费的时间相比,在 error_log 中花费的时间将是巨大的。

关于PHP error_log 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1600124/

相关文章:

php - 将变量从 View 传递到 Controller

java - 正则表达式正好 n 或 m 次

php - Facebook 用户取消对应用程序的授权

php - 从它自己的方法调用 PHP 构造函数

c++ - 为什么在 C++ 中的类初始化之前使用作用域运算符 (::)?

java - 使用Tomcat时如何避免在Eclipse/NetBeans中编写Java代码?

r - R : cannot open file 'specdata/001.csv' : No such file or directory [duplicate]

php - 简单的php登录系统

php - Laravel验证数字多维数组

java - 在创建自定义未检查异常时扩展 Error 或 runtimeException