11

I have this PHP code :

error_log('my message 1');
....
error_log('my message 2');
...
error_log('my message 3');

This produces in apache error_log one line with all messages :

[Wed Nov 13 17:24:55.880399 2013] [proxy_fcgi:error] [pid xx] [client xxx] AH01071: Got error 'PHP message: my message 1\n'PHP message: my message 2\n'PHP message: my message 3

My config :

Apache 2.4
PHP : 5.4
PHP-FPM with proxypassmatch directive.

My question : Why messages are on the same line, and how to do to have one line per message ?

Thanks for yours answers.

EDIT

One line per message should look like :

[Wed Nov 13 17:24:55.880399 2013] [proxy_fcgi:error] [pid xx] [client xxx] AH01071: Got error 'PHP message: my message 1'
[Wed Nov 13 17:24:55.880399 2013] [proxy_fcgi:error] [pid xx] [client xxx] AH01071: Got error 'PHP message: my message 2'
[Wed Nov 13 17:24:55.880399 2013] [proxy_fcgi:error] [pid xx] [client xxx] AH01071: Got error 'PHP message: my message 3'

2 Answers2

1
error_log("error message \r\n");

PHP disregards special ASCII characters within single quotes(it renders it as separate chars), you need to use double quotes.

In addition: You should open your php.ini file, the one in the /etc/php5/apache2/ folder, and chnage the error_log directive to point to a file.

It is important that Apache will have sufficient privileges to write into this file. so chown www-data:www-data /var/www/somefile.log should do it

If it's currently undefined, the logs will go through syslog, and there new lines are not allowed.

Additional edit: To penetrate output buffering you need to raise an exception.

example:

try{
  ob_start();
  doSomething($userInput);
  ob_end_flush();
}
catch(Exception $e){
 error_log($e->getMessage());
}

function doSomething($data = null){
  if($data === null){
    throw new Exception("Data is required");
  }
  else{
    //do something
  }

}
Oleg Belousov
  • 9,981
  • 14
  • 72
  • 127
  • Doesn't work. Prints `[Thu Nov 14 10:07:50.850010 2013] [proxy_fcgi:error] [pid xx] [client xx] AH01071: Got error 'PHP message: my message 1 \r\n\nPHP message: my message 2 \r\n\nPHP message: my message 3 \r\n\n', referer:` – Marc Elbichon Nov 14 '13 at 09:08
  • Which machine is it?, is it a debian?. – Oleg Belousov Nov 14 '13 at 09:13
  • centos. I think this is due to mod_fastcgi or mod_proxy because works with mod_php – Marc Elbichon Nov 14 '13 at 09:22
  • 1
    Works when i set error_log directive. But, i want to have only one error log file (apache + php). According to [documentation](http://us1.php.net/manual/en/errorfunc.configuration.php#ini.error-log), when error_log is not defined, errors are sent to SAPI error logger(not syslog) so to apache log. In this case, seems errors are buffered and sent to apache at the end of the script. – Marc Elbichon Nov 14 '13 at 09:35
  • It really depends of the setup you are running(on debian the logs are definitely going through sys log) according to my information, and to the behavior that you have just witnessed, you have no other choice but to separate your own logs into a different file. – Oleg Belousov Nov 14 '13 at 10:28
  • This is a better structure because this way to isolate your own errors from the internal server ones, and your log becomes more structured and readable, if you still want to have one log for everything, you can setup a cron that will read your custom log file every minute of so, append it's contents to the end of your main log file, and then erase it. – Oleg Belousov Nov 14 '13 at 10:28
  • OK. Do you know why messages are buffered before printing ? – Marc Elbichon Nov 14 '13 at 15:24
  • Are you using the php ob_ functions? – Oleg Belousov Nov 14 '13 at 16:23
  • Please elaborate, what exactly you problem now? Was the original problem solved? – Oleg Belousov Nov 16 '13 at 15:07
  • 1
    The first question was when i execute several error_log calls, they were concatenated into one in the error log. I want one line per error_log call (with pattern) in the error log. – Marc Elbichon Nov 26 '13 at 10:42
  • 3
    @MarcElbichon Did the concatenation issue ever get resolved? I'm running in to the same issue and my searches keep coming back here. – J.D. Pace Oct 14 '20 at 15:14
  • Guys, in PHP your cannot add escaped characters in single quotes - still true. Please see below - there are several solutions. https://stackoverflow.com/questions/2531969/print-newline-in-php-in-single-quotes – Oleg Belousov Nov 16 '22 at 21:46
0

User \r\n

error_log("my message 1\r\n");
....
error_log("my message 2\r\n");
...
error_log("my message 3\r\n");
Diemuzi
  • 3,507
  • 7
  • 36
  • 61
TaylorSmolik
  • 116
  • 3
  • 13