douwen6274 2014-05-22 09:49
浏览 48
已采纳

使用换行符将PHP错误报告到屏幕

I have setup error reporting in the top of my script to

// 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

The reporting is being logged/saved to my error log file with line breaks, but displays on the screen as 1 continuous line without breaks.

Screen-output example:

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

Error-log file example:

[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

Is there any way to make the output to screen display with line breaks too, so that it will be easier to read it?

Thank you.

  • 写回答

1条回答 默认 最新

  • doucheng2210 2014-05-22 09:55
    关注

    You can use line breaks or HTML in your code to drop in what you need between warnings and errors:

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

    Will output:

    some warningsome warning
    

    However, you can add a BR to it to display properly in a browser:

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

    Which will output the following in a browser:

    some warning
    some warning
    

    Alternately, you can use non html line breaks if you are using a terminal, or outputting text to the screen (rather than HTML):

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

    Which will put in line breaks into normal text.

    You can mix and match these together to suit your needs. The other thing you can do is use <pre> and </pre> tags around your error messages so that they display into a browser and/or text file as the error message itself reads:

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

    Edit: If you are generating many warnings/errors that you aren't actually displaying on purpose, you can also modify the following:

    html_errors = 1
    

    in your PHP.ini file which will output things into a nice HTML format.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题