PHP通知自定义格式

cjb*_*rth 0 php error-handling php-7

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

有没有办法PHP Notice在 PHP >= 7 中自定义消息?

Dav*_*ave 5

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

[2019 年 2 月 27 日 13:55:09 America/New_York] 8 未定义变量:来自 URI /customnotice.php 的 hello

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
Run Code Online (Sandbox Code Playgroud)