我正在寻找一种方法,用一行代码自动记录所有错误。如果您了解Asp.NET,您可能知道我的意思,比如使用Application_Error
事件处理程序。
我检查了PHP日志框架?SO中的问题,但它们看起来都一样,你应该手动记录每一条日志消息,这意味着我需要在任何我想记录的地方调用日志函数。
我要找的不是像这样的东西(由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
内部的内容进行处理