phperror-reportingerror-log

PHP Error Reporting to Screen, with Line Breaks


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.


Solution

  • 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."\r\n";
    echo $error."\r\n";
    

    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.