erikbstack
erikbstack

Reputation: 13254

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.

Upvotes: 6

Views: 3225

Answers (5)

pivasikkost
pivasikkost

Reputation: 37

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.

Upvotes: 1

Adam Genshaft
Adam Genshaft

Reputation: 824

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 the second argument and a file path as the third argument.

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

For more information, check out PHP error_log documentation.

Upvotes: 0

Justin Nafe
Justin Nafe

Reputation: 3052

If you are viewing the output in the browser, try wrapping your 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
)

Upvotes: 0

Kishorevarma
Kishorevarma

Reputation: 956

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

Upvotes: 11

Andrey Usov
Andrey Usov

Reputation: 1587

In some cases (e.g. Mac) using Perl might work better:

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

Upvotes: 1

Related Questions