我正在寻找一种方法,可以使用一行代码自动记录所有错误。如果您了解Asp.NET,则可能知道我的意思,就像使用Application_Error
事件处理程序一样。
我在SO中检查了PHP Logging framework?问题,但是它们看起来都一样,您应该手动记录每条日志消息,这意味着我需要在要记录的任何地方调用log函数。
我正在寻找的不是这样的东西(由KLogger使用)
require_once 'KLogger.php';
...
$log = new KLogger ( "log.txt" , KLogger::DEBUG );
// Do database work that throws an exception
$log->LogError("An exception was thrown in ThisFunction()");
// Print out some information
$log->LogInfo("Internal Query Time: $time_ms milliseconds");
// Print out the value of some variables
$log->LogDebug("User Count: $User_Count");
最佳答案
您可以在PHP中创建自己的自定义错误处理函数,并将其设置为错误处理函数
就像是:
function handle_error($error_level,$error_message)
{
/*
here do what you want...typically log it into db/file/send out
emails based on error level etc..
*/
}
并将此功能设置为PHP的默认错误处理程序,请添加以下行:
set_error_handler("handle_error");
PHP现在将根据
handle_error
中编写的内容处理所有错误
关于php - 自动记录PHP中的所有错误,警告和通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12103442/