PHP错误报告到屏幕,带有换行符


PHP Error Reporting to Screen, with Line Breaks

我在脚本的顶部设置了错误报告

// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');  //On or Off

报告正在被记录/保存到我的错误日志文件中,但在屏幕上显示为连续的一行,没有换行符。

屏幕输出示例:

Warning: getimagesize(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 48 Warning: Division by zero in /home/xyzxyz/public_html/fetcher.php on line 49 Warning: imagecreatefromjpeg(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 62 Warning: imagecreatetruecolor(): Invalid image dimensions in /home/xyzxyz/public_html/fetcher.php on line 64 Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 66 Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 72

错误日志示例:

[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning:  getimagesize(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 48
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning:  Division by zero in /home/xyzxyz/public_html/fetcher.php on line 49
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning:  imagecreatefromjpeg(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 62
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning:  imagecreatetruecolor(): Invalid image dimensions in /home/xyzxyz/public_html/fetcher.php on line 64
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning:  imagecopyresampled() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 66
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning:  imagejpeg() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 72

是否有办法使输出的屏幕显示与换行,以便它将更容易阅读?

谢谢。

您可以在代码中使用换行符或HTML在警告和错误之间放置您需要的内容:

$error = "some warning";
echo $error;
echo $error;

将输出:

some warningsome warning

但是,您可以为它添加BR以便在浏览器中正确显示:

$error = "some warning";
echo $error."<br />";
echo $error."<br />";

将在浏览器中输出以下内容:

some warning
some warning

或者,如果您使用的是终端,或者将文本输出到屏幕(而不是html),则可以使用非html换行符:

$error = "some warning";
echo $error."'r'n";
echo $error."'r'n";

这将把换行变成正常的文本。

你可以混合搭配这些来满足你的需要。您可以做的另一件事是在错误信息周围使用<pre></pre>标签,以便它们显示在浏览器和/或文本文件中,如错误信息本身所示:

$error = "some warning";
echo "<pre>";
echo $error;
echo $error;
echo "</pre>";

编辑:如果你产生了许多警告/错误,而你实际上并不是故意显示的,你也可以修改以下内容:

html_errors = 1
在PHP.ini文件中的

,它将输出成一个漂亮的HTML格式。

为了在浏览器中正确显示换行,我在index.php (PHP7.4)中设置了以下代码

ini_set('error_prepend_string', '<pre>');
ini_set('error_append_string', '</pre>');
ini_set('display_errors', 1);

此外,为了将文本换行到屏幕宽度,将第一个ini_set替换为

ini_set('error_prepend_string', '<pre style="white-space: pre-wrap;">');