0

我将一些 Web 应用程序从 PHP 5.6 迁移到 PHP 7.0,但我注意到未处理的异常不会记录在配置的日志中(或至少在 apache 日志 error.log 中)。错误消息不会显示在网站上,它只会在错误出现后停止。

我用那个简单的代码试了一下:


<?php
namespace Application\MyApp;
    
ini_set('error_log', '/my/log/location/my_error.log');
error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
    
$host = gethostname();
echo "<b>start test on $host</b><br>";
    
throw new \Error('Test exception outside of try/catch.');
    
echo "<b>test finished on $host<b><br>";

使用 PHP 5.6,我在配置的日志中使用相同的代码得到预期的错误消息:

PHP Fatal error:  Uncaught exception 'Error' with message 'Test exception outside of try/catch.

是否可以将所有未捕获的异常重定向到配置的日志文件中,就像在 PHP 5.6 中一样?

4

1 回答 1

0

如果添加异常处理程序方法,我将获得所需的日志记录消息:

function myExceptionHandler (\Throwable $ex)
{
    echo $ex->getMessage();
    throw $ex;
}
set_exception_handler('Application\MyApp\myExceptionHandler');
于 2021-08-02T07:14:39.747 回答