简体   繁体   English

PHP error_log性能问题

[英]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. 问题是,当我分析应用程序时,它会阻塞error_log函数。 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. 您可以将错误信息存储在数据库中,然后让cron脚本通过电子邮件向您发送内容。 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. 在检测位置的条件中包装方法调用可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM