我在PHP 5中使用了一个将错误转换为异常的类,它会将错误记录到文件中并/或将其发送到指定账户的电子邮件。有没有更好的方法来做这件事?我知道这方面有些需要改进。我正在使用set_error_handler。
set_error_handler("exception_error_handler");
我的代码会记录和发送错误信息,但我是否以最佳方式处理这个流程?将其记录到数据库中是否更好——假设错误时存在数据连接。在网站方面,业界标准是什么?
我在PHP 5中使用了一个将错误转换为异常的类,它会将错误记录到文件中并/或将其发送到指定账户的电子邮件。有没有更好的方法来做这件事?我知道这方面有些需要改进。我正在使用set_error_handler。
set_error_handler("exception_error_handler");
我的代码会记录和发送错误信息,但我是否以最佳方式处理这个流程?将其记录到数据库中是否更好——假设错误时存在数据连接。在网站方面,业界标准是什么?
你处理错误的代码必须绝对无懈可击。
有时它会突然起作用,因为你忘记测试一个非常模糊的原因,但即使在代码末日的震荡中,你仍然希望它能正常运行。
将写入数据库的输出作为代码的一部分会导致非常大的依赖关系,而缺乏数据库很可能会是报告的主要问题之一。
依赖邮件仍然是一种依赖性,但在系统故障的情况下,最紧急的目标应该是使系统再次正常运行 - 因此,发送电子邮件是一种非常有效的通知您需要修复某些内容的方式。
PHP 的文件处理功能不适合并发访问 - 因此,虽然我建议在本地记录任何事件,但不要从代码中编写文件 - 使用 syslog 接口。在您发送到 syslog 之后,可以通过电子邮件发送相关详细信息。
HTH: 希望有所帮助。
C. (this is already in English and does not need to be translated)
我不会将那个逻辑倒入错误处理程序中。
看看这个来自Kohana的方法。
/**
* PHP error handler, converts all errors into ErrorExceptions. This handler
* respects error_reporting settings.
*
* @throws ErrorException
* @return TRUE
*/
public static function error_handler($code, $error, $file = NULL, $line = NULL)
{
if (error_reporting() & $code)
{
// This error is not suppressed by current error reporting settings
// Convert the error into an ErrorException
throw new ErrorException($error, $code, 0, $file, $line);
}
// Do not execute the PHP error handler
return TRUE;
}
干净并且做了方法描述的工作。现在你可以把你的处理移动到异常处理程序或者一个 catch 块内部。
The title is horrible, i know; I m terrible at titles on SO here. I m wondering what would be the best way to present unified error responses in a webapi when errors could be raised deep inside the ...
I had an exception in some code today: "A [some exception] was unhandled." However, this code was clearly inside the "try" block of a "try/catch" structure. What am I missing here? Update: It s C# ...
I need to be able to instantiate an object of a class in Dojo at runtime and mix it into another object (kind of like specifying an extends or an implements in Java, but at runtime). and I came up ...
I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...
File file = new File(path); if (!file.delete()) { throw new IOException( "Failed to delete the file because: " + getReasonForFileDeletionFailureInPlainEnglish(file)); } Is there a ...
I m looking for a way to continue execution of a transaction despite errors while inserting low-priority data. It seems like real nested transaction could be a solution, but they aren t supported by ...
I m trying to standardize the way I handle exceptions in my web application (homemade framework) but I m not certain of the "correct" way to handle various situations. I m wondering if there is a ...
I m having an issue with custom errors on an ASP.NET MVC app I ve deployed on my shared host. I ve created an ErrorController and added the following code to Global.asax to catch unhandled exceptions, ...