php - PHP注意事项自定义格式

标签 php error-handling php-7

当PHP向日志发出PHP Notice时,它包含出现问题的文件名和行号。对于大型应用程序,这通常不足以重现该问题。真正有用的是一些其他信息,最明显的是在此通知发生时被调用的URL。

有没有一种方法可以在PHP> = 7中自定义PHP Notice消息?

最佳答案

创建您自己的错误处理程序并捕获通知,然后使用所需信息记录消息。下面的代码生成以下PHP错误日志文件。

[27-Feb-2019 13:55:09 America/New_York] 8 Undefined variable: hello from URI /customnotice.php


function myErrorHandler($errno,$errstr, $errfile, $errline) {

    if ($errno == 8) {  // this is a notice
        error_log($errno . ' ' . $errstr . ' from URI ' . $_SERVER['REQUEST_URI']);
    }
}

$old_error_handler = set_error_handler("myErrorHandler");

echo $hello;  // will throw a notice for testing

关于php - PHP注意事项自定义格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54906991/

相关文章:

python - 处理Python中的记录器错误

php - 查找 PHP 中何时引入内部(内置)函数和预定义常量(版本号)

php - Laravel - 按月模型过滤日期字段

php - 使用 JSON 文件而不是数据库?

php - 从 authorize.net 获取交易详情

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

error-handling - 我对以下Rust “reqwest”代码的理解正确吗?

php - mysqli 中的 mysql_field_names 使用循环

php - 使用 ini_set() 的 PSR-1 兼容替代方案是什么?

PHP - mysql_fetch_array 如何给我选定的返回值?