ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP:忽略特定类中的错误和通知

2019-07-01 01:16:56  阅读:227  来源: 互联网

标签:php exception-handling error-handling simplepie


我的网站是完全自定义的,因此我想知道我编写的代码写得不好.我使用set_exception_handler和set_error_handler来使用自定义类将错误记录到文件中.这包括通知和警告.

在我自己的代码中,这很好,因为我得到的日志很少,而我得到的是我真正想要解决的问题.

但是,我刚刚开始使用simplePie,因为它与PHP4兼容,我收到了大量的通知,主要用于静态调用函数或通过引用错误地传递内容.

对我来说,通过修复simplePie太过分了,如果不是,我首先不会使用它.

有没有办法可以专门忽略某个文件或类生成的错误?以下是我的基本异常处理内容的概述:

    set_exception_handler("CustomExceptionHandler");
    set_error_handler("customErrorHandler");

    /**
     * CustomExceptionHandler()
     *
     * This is used if an exception is thrown that isn't caught.
     *
     * @param   object  $e  The exception as an object
     */
    function CustomExceptionHandler(Exception $e) {
        exitToError($e->getMessage());
    }

    /**
     * customErrorHandler()
     *
     * This is called for any errors no matter what their level.
     */
    function customErrorHandler($errno, $errstr, $errfile, $errline) {
        if(in_array($errno, array(E_USER_ERROR, E_RECOVERABLE_ERROR))) {
            throw new CustomErrorException($errstr, 0, $errno, $errfile, $errline);
        } else {
            CustomException::logError($errstr, $errno, $errfile, $errline);
        }
        return FALSE;
    }

/**
     * class CustomErrorException
     *
     * Used by custom_error_handler() to convert all fatal
     * errors to exceptions.
     *
     * @see custom_error_handler()
     * @see http://www.php.net/manual/en/class.errorexception.php
     */
    class CustomErrorException extends CustomException {
        /**
         * $severity
         *
         * The severity level of the exception
         *
         * @access  protected
         * @var     int
         */
        protected $severity;

        /**
         * __construct()
         *
         * Constructs the new exception
         *
         * @access  public
         * @param   string  $message    The Exception message
         * @param   int     $code       The Exception code
         * @param   int     $severity   The severity level of the exception
         * @param   string  $filename   The filename where the exception was thrown
         * @param   int     $lineno     The line number where the exception was thrown
         */
        public function __construct($message, $code = null, $severity = E_ERROR, $filename = null, $lineno= null) {
            $this->message  = $message;
            $this->code     = $code;
            $this->severity = (int)$severity;
            $this->file     = $filename;
            $this->line     = $lineno;

            self::logError($this->message,$this->code,$this->file,$this->line,$this->getTraceAsString());
        }
    }

    /**
     * class CustomException
     *
     * Overwrites Exception to give us more control on how
     * exceptions are handled and logged.
     *
     * @see http://www.php.net/manual/en/language.exceptions.extending.php
     */
    class CustomException extends Exception {

        /**
         * __construct
         *
         * We call the parent contruct as we still want it to do all its magic. We just want
         * overwrite this method so that we can log the error exactly how we want.
         */
        public function __construct($message, $code = 0, Exception $previous = NULL) {
            parent::__construct($message, $code);
            self::logError($this->getMessage(),$this->getCode(),$this->getFile(),$this->getLine(),$this->getTraceAsString());
        }

        /**
         * __toString()
         *
         * We overwrite this function so that we can use our stringBuilder function.
         */
        public function __toString() {
            return self::stringBuilder($this->getMessage(),$this->getCode(),$this->getFile(),$this->getLine(),$this->getTraceAsString());
        }

        /**
         * stringBuilder()
         *
         * We use this method so that we have a standard method of building error
         * strings that anything can tap into.
         *
         * @access  public
         * @param   string  $message    the exception message
         * @param   int     $code       the code assigned to this exception
         * @param   string  $file       the file where the exception occurred
         * @param   int     $line       the line where the exception occurred
         * @param   string  $trace      backtrace
         */
        public function stringBuilder($message, $code, $file, $line, $trace='') {
            //return "[".date("d-M-Y H:i:s")."] ".$this->getMessage()." in ".$this->getFile().":".$this->getLine()."\nStack trace:\n".$this->getTraceAsString()."\n";
            return "[".date("d-M-Y H:i:s")."] ".$message." in ".$file.":".$line."\n";
        }

        /**
         * logError()
         *
         * We use a method so that we have a standard way of saving errors
         * to a log.
         *
         * We use XML because it's easy to parse.
         *
         * @access  public
         * @param   string  $message    the exception message
         * @param   int     $code       the code assigned to this exception
         * @param   string  $file       the file where the exception occurred
         * @param   int     $line       the line where the exception occurred
         * @param   string  $trace      backtrace
         * @todo    We could improve it to write to the xml file using DomDocument
         *          as laid out here http://www.xml-training-guide.com/append-delete-data-from-xml-using-php.html
         */
        public function logError($message, $code, $file, $line, $trace='') {
            //Save it to a standard text file to guarentee saving the error
            file_put_contents(ROOT_URL.ERROR_LOG_TXT,self::stringBuilder($message, $code, $file, $line, $trace),FILE_APPEND);
        }
    }

这是simplePie引发的两个错误的示例:

[01-Aug-2010 00:50:33] Assigning the return value of new by reference is deprecated in ***\SimplePie.php:738
[01-Aug-2010 00:50:34] Non-static method SimplePie_Misc::parse_date() should not be called statically in ***\SimplePie.php:60

解决方法:

Is there a way that I can specifically ignore errors generated by a certain file or class?

应该很容易!您可以签入自定义错误处理程序

function customErrorHandler($errno, $errstr, $errfile, $errline)

$errfile是否在其中一个simplePie文件中,并且在这种情况下返回true;默默.

标签:php,exception-handling,error-handling,simplepie
来源: https://codeday.me/bug/20190701/1342073.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有