16

What's the PHP config setting which allows or prevents newlines in debug output from being escaped?

On two different installs (a dev laptop running MAMP/OSX, and a dev server running debian) I see different results in the error logs when debugging.

error_log(print_r(array(1,2,4),1));

On Debian this appears in /var/log/apache2/error.log as

[Thu Jul 30 11:32:34 2009] [error] [client 118.93.246.104] Array\n(\n    [0] => 1\n    [1] => 2\n    [2] => 4\n)\n, referer: http://dev.example.org/

On OSX this appears in /Applications/MAMP/logs/php_error_log as

[30-Jul-2009 11:34:00] Array
(
    [0] => 1
    [1] => 2
    [2] => 4
)

I prefer the MAMP way for debugging (apart from relocating logfiles to the /Applications directory).

Thanks!

Chris Burgess
  • 3,551
  • 3
  • 29
  • 42

2 Answers2

15

Chris, you should be able to change the error_log directive in your php.ini on Debian to point to a file. If this is undefined, it will go through syslog which doesn't support multiple lines.

Details:

error_log function

error_log directive

hobodave
  • 28,925
  • 4
  • 72
  • 77
  • It should also be mentioned that if the user that Apache runs as cannot write to the specified error_log file (due to permissions problems), it will also go to syslog or Apache log. – Pistos Aug 19 '09 at 04:50
2

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.

devrys
  • 1,571
  • 3
  • 27
  • 43
regexp
  • 766
  • 4
  • 14
  • 3
    Wary of setting go+x on a file that attackers can write to. Set group read/write and group ownership to the webserver user instead. Be careful of permissions changes getting unset on log rotation (chmod setting in logrotate config, etc). – Chris Burgess Nov 13 '12 at 21:52
  • Agree that this is somewhat insecure (though I hope your log files are not accessible through the web server...) but it is a fine quick for a local dev environment and it points out the key issue, which is that the permissions of the log file can prevent Apache from writing to it, causing syslog to be used instead. – jrz Apr 21 '13 at 00:03
  • 1
    My own solution was more like Chris Burgess's recommendation, I added my user to the group the webserver runs as, changed the file's permissions to allow full access to the group, and changed ownership of the file to user/group that the webserver runs as, i.e on OSX with the pre-installed Apache: ```sudo dseditgroup -o edit -a myusername -t user _www; sudo chown _www:_www /var/log/apache2/error_log; sudo chmod 770 /var/log/apache2/error_log; ``` – jrz Apr 21 '13 at 00:10
  • Can't get this to work: running Ubuntu 16.04, Apache2 as `www-data` (user and group), vhost config with `ErrorLog ${APACHE_LOG_DIR}/myapp.error.log`, `/var/www/apache2` with 755, and `myapp.error.log` owned by `www-data` user (and group). On top permissions testwise set to 777 on that file. Apache restarted. Still dumping `\n` in that file. What am I missing, any ideas? – robbash Sep 03 '16 at 09:38