Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Several error_log() messages on same line in error log

Tags:

php

apache

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'
like image 327
Marc Elbichon Avatar asked Nov 13 '13 16:11

Marc Elbichon


Video Answer


1 Answers

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
  }

}
like image 187
Oleg Belousov Avatar answered Oct 15 '22 23:10

Oleg Belousov