简体   繁体   中英

PHP/Apache error.log doesn't execute new_line characters, why?

For example instead of getting the following

post:Array (
  "a" => "b",
  "c" => "d"
)

I just get this:

post:Array (\n  "a" => "b",\n  "c" => "d"\n)

It's really uncomfortable to read this while debugging my code. So if you have any suggestion on why this couldn't work alright, tell me.

I am running it in a Windows7 Putty connected to an Ubuntu virtual server, which runs supposedly it's default Apache/PHP configuration. (well probably not, but as always nobody in the team remembers to have changed anything)

edit: Someone requested the code that writes to the error.log:

<?php
error_log(print_r(array("a"=>"b","c"=>"d"),1));
?>

The commands to view the error log are:

sudo tail -f /var/log/apache2/error.log
sudo vim /var/log/apache2/error.log
sudo cat /var/log/apache2/error.log

In all instances the problem occurs that \\n is not executed as expected.

I also faced the same problem, but after spending a few minutes I got a solution.

When you do tail use as below

sudo tail -f /var/log/apache2/error.log | sed -e 's/\\n/\n/g'

If you want you can create a file, give it some name and paste the above command and place that in /usr/bin/ folder

For example

vi tailme

With the contents:

#!/bin/bash
tail -f /var/log/apache2/error.log | sed -ue 's/\\n/\n/g'

and put this in /usr/bin/ now you can use tailme as a command.

在某些情况下(例如 Mac),使用 perl 可能会更好:

tail -100f /var/log/apache2/error.log | perl -pe 's/\\n/\n/g'

The problem is caused when the Apache process can't write into the error_log file, so the syslog writes into the file instead. The syslog messes up the line breaks.

So just do:

chmod 777 error.log

This should solve your problem. Source

When calling error_log , You can force php to log directly instead of passing it to apache which is the default behavior. To do that just specify 3 as a second argument and a file path as the third argument.

error_log("error message", 3, $logFileLocation);

For more info check out PHP error_log docs .

If you are viewing the output in the browser, try wrapping you output statement with the <pre> tags.

<?php
$post = Array("a" => "b", "c" => "d");
echo "<pre>";
print_r($post);
echo "</pre>";
?>

outputs to a browser a formatted array

Array
(
    [a] => b
    [c] => d
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM