php - 自动记录PHP中的所有错误,警告和通知

标签 php error-handling

我正在寻找一种方法,可以使用一行代码自动记录所有错误。如果您了解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/

相关文章:

php - 使用不带引号的数组键,如何在大项目中修复它?

php - 我如何设置默认默认图像 (no_pic.gif) php?

c# WPF datagrid数据绑定(bind)错误

ajax - Node.js Express-根据错误类型不同地处理错误

javascript - 停止搜索引擎索引页面的特定部分

php - mySQL 查询不返回 PHP 中的串联结果

php - JMSSerializer 和 UTF-8 编码错误 (Symfony2.4)

node.js - Expressjs - 使用中间件处理错误

c# - Linq - 无法隐式转换类型'System.Collections.Generic.IEnumerable

windows - 捕获命令的输出并检查批处理脚本中的错误级别