我的网站是完全自定义的,因此我想知道我的代码何时写得不好。我使用 set_exception_handler 和 set_error_handler 来使用自定义类将错误记录到文件中。这包括通知和警告。
在我自己的代码中,这很好,因为我得到的日志很少,而我确实得到的是我真正想要修复的东西。
但是,我刚刚开始使用 simplePie,因为它与 PHP4 兼容,所以我收到了很多通知,主要是针对静态调用函数或不正确地通过引用传递事物之类的事情。
对我来说,修复 simplePie 实在是太麻烦了,如果不是,我一开始就不会使用它。
有没有办法可以专门忽略某个文件或类生成的错误?这是我非常基本的异常处理的概述:
set_exception_handler("CustomExceptionHandler");
set_error_handler("customErrorHandler");
/**
* 自定义异常处理程序()
*
* 如果抛出未捕获的异常,则使用此选项。
*
* @param object $e 作为对象的异常
*/
函数自定义异常处理程序(异常 $e){
exitToError($e->getMessage());
}
/**
* 自定义错误处理程序()
*
* 无论级别如何,任何错误都需要这样做。
*/
函数 customErrorHandler($errno, $errstr, $errfile, $errline) {
if(in_array($errno, array(E_USER_ERROR, E_RECOVERABLE_ERROR))) {
throw new CustomErrorException($errstr, 0, $errno, $errfile, $errline);
} 别的 {
CustomException::logError($errstr, $errno, $errfile, $errline);
}
返回错误;
}
/**
* 类自定义错误异常
*
* custom_error_handler() 用于转换所有致命错误
* 异常的错误。
*
* @see custom_error_handler()
* @see http://www.php.net/manual/en/class.errorexception.php
*/
类 CustomErrorException 扩展 CustomException {
/**
* $严重性
*
* 异常的严重程度
*
* @access 受保护
* @var 整数
*/
受保护的$严重性;
/**
* __construct()
*
* 构造新的异常
*
* @访问公共
* @param string $message 异常消息
* @param int $code 异常代码
* @param int $severity 异常的严重程度
* @param string $filename 抛出异常的文件名
* @param int $lineno 抛出异常的行号
*/
公共函数 __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());
}
}
/**
* 类自定义异常
*
* 覆盖异常,让我们更多地控制如何
* 异常被处理和记录。
*
* @see http://www.php.net/manual/en/language.exceptions.extending.php
*/
类 CustomException 扩展异常 {
/**
* __构造
*
* 我们调用父结构,因为我们仍然希望它发挥所有魔力。我们只想
* 覆盖这个方法,这样我们就可以准确地记录我们想要的错误。
*/
公共函数 __construct($message, $code = 0, 异常 $previous = NULL) {
父::__construct($message, $code);
self::logError($this->getMessage(),$this->getCode(),$this->getFile(),$this->getLine(),$this->getTraceAsString());
}
/**
* __toString()
*
* 我们重写了这个函数,以便我们可以使用我们的 stringBuilder 函数。
*/
公共函数 __toString() {
return self::stringBuilder($this->getMessage(),$this->getCode(),$this->getFile(),$this->getLine(),$this->getTraceAsString());
}
/**
* 字符串生成器()
*
* 我们使用这种方法,以便我们有一个标准的构建错误的方法
* 任何东西都可以使用的字符串。
*
* @访问公共
* @param string $message 异常信息
* @param int $code 分配给这个异常的代码
* @param string $file 发生异常的文件
* @param int $line 发生异常的行
* @param string $trace 回溯
*/
公共函数 stringBuilder($message, $code, $file, $line, $trace='') {
//return "[".date("dMY H:i:s")."] ".$this->getMessage()." in ".$this->getFile().":".$this- >getLine()."\n堆栈跟踪:\n".$this->getTraceAsString()."\n";
return "[".date("dMY H:i:s")."] ".$message." in ".$file.":".$line."\n";
}
/**
* 日志错误()
*
* 我们使用一种方法,以便我们有一个标准的方法来保存错误
* 到日志。
*
* 我们使用 XML 是因为它易于解析。
*
* @访问公共
* @param string $message 异常信息
* @param int $code 分配给这个异常的代码
* @param string $file 发生异常的文件
* @param int $line 发生异常的行
* @param string $trace 回溯
* @todo 我们可以改进它以使用 DomDocument 写入 xml 文件
* 如此处所述 http://www.xml-training-guide.com/append-delete-data-from-xml-using-php.html
*/
公共函数 logError($message, $code, $file, $line, $trace='') {
//将其保存到标准文本文件以保证保存错误
file_put_contents(ROOT_URL.ERROR_LOG_TXT,self::stringBuilder($message, $code, $file, $line, $trace),FILE_APPEND);
}
}
这是 simplePie 抛出的两个错误的示例:
[01-Aug-2010 00:50:33] 通过引用分配 new 的返回值在 ***\SimplePie.php:738 中已弃用 [01-Aug-2010 00:50:34] 不应在 ***\SimplePie.php:60 中静态调用非静态方法 SimplePie_Misc::parse_date()