Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 603
erikbstack Avatar asked Dec 28 '22 09:12

erikbstack


2 Answers

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.

like image 125
Kishorevarma Avatar answered Dec 29 '22 21:12

Kishorevarma


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

tail -100f /var/log/apache2/error.log | perl -pe 's/\\n/\n/g'
like image 44
aux Avatar answered Dec 29 '22 23:12

aux