PHP自定义错误处理程序类记录/显示重复错误

问题描述

这是我第一次使用自定义错误处理并决定采用创建类的途径。目前可以使用,但有副作用。

当前ini值:

error_log = errors.log
display_errors = 0
log_errors = 1

PHP代码行:

include('fakefile.PHP');

产生错误

WARNING:  
    Code: 2
    Message: include(fakefile.PHP): Failed to open stream: No such file or directory
    File: F:\laragon\www\projects\JLDesignNetwork\index.PHP
    Line: 71

WARNING:  
    Code: 2
    Message: include(): Failed opening 'fakefile.PHP' for inclusion (include_path='.;F:/laragon/etc/PHP/pear')
    File: F:\laragon\www\projects\JLDesignNetwork\index.PHP
    Line: 71

error_handler.class.PHP

class ErrorHandler
{
    private $mailable = array(E_ERROR,E_USER_ERROR,E_USER_WARNING,E_WARNING);
    private $friendlyMsg = "We have encountered an unexpected error. The administrator has been notified. Please check back later.";
    private $logfile = null;
    private $techMail = null;
    private $mute = false;
    private $debug = false;
    private $details = null;
        
    function __construct($logfile=null,$techMail=null,$mute=false,$debug=false)
    {
        $this->logfile = $logfile; // should check to see if file exists
        $this->techMail = $techMail;  // should verify email is in valid format
        $this->mute = $mute;
        $this->debug = $debug;
        
        ini_set('error_log',$logfile);
        set_error_handler(array($this,'customHandler'));
    }
    
    public function customHandler($errno,$errstr,$errfi,$errli)
    {
        $this->logstr = json_encode([
            "datetime" => date(DATE_RFC2822),"error" => [
                "code" => $errno,"file" => $errfi,"line" => $errli,"msg" => $errstr
            ]
        ],JSON_FORCE_OBJECT & JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UnesCAPED_UNICODE);
            
        $this->details = array($errno,$errli);
        $this->response();
        
        if(in_array($errno,$this->mailable)): $this->sendMail(); endif;
    }
        
    private function response() 
    {
        error_log(
            $this->logstr. PHP_EOL,3,$this->logfile
        );
            
        $display = sprintf(
            "\tCode: %d\n\tMessage: %s\n\tFile: %s\n\tLine: %d</pre>",$this->details[0],$this->details[1],$this->details[2],$this->details[3]
        );
            
        // display an error message; shutdown if needed
        switch($this->details[0]):
            case E_USER_ERROR:
            case E_ERROR:
                $str = $this->debug ? "<pre><b>FATAL:</b> <br>" . $display : $this->friendlyMsg;
                break;
            case E_USER_WARNING:
            case E_WARNING:
                $str = $this->debug ? "<pre><b>WARNING:</b>  <br>" . $display  : $this->friendlyMsg;
                break;
            case E_USER_NOTICE:
            case E_NOTICE:
                $str = $this->debug ? "<pre><b>NOTICE:</b>  <br>" . $display  : $this->friendlyMsg;
                break;
            default :
                // do nothing
                break;
        endswitch;
            
        if(!$this->mute): echo $str; endif;
    }
        
    private function sendMail()
    {
        $html = sprintf("<h3>An error has occurred!</h3><hr>The following error was recently logged: <pre>%s</pre>",print_r(json_decode($this->logstr),true));
        /*error_log(
            $html,1,$this->techMail,'Content-type: text/html; charset=iso-8859-1'
        );*/
    }
}
    
$eh = new ErrorHandler('errors.log','someone@somewhere.net',false,true);

我的问题是->为什么包含错误两次记录并显示(在屏幕上)?

请记住,这不是实时的,仍在进行中。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...