【发布时间】:2021-10-05 18:46:37
【问题描述】:
我将一些 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 中一样?
【问题讨论】: