Php – Why is this apache php piped log script not working properly

apache-2.2loggingPHPpipe

i have a piped logging program like this:

#!/usr/bin/php
<?php

$fd = fopen("php://stdin", "r");
while(true) {
  $l = fread($fd,8192);
  $l = trim($l);
  if($l =="") continue;
  file_put_contents('/home/proxy/testfile',$l."\n", FILE_APPEND);
}
fclose($fd);

and in my webserver config i have:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" proxylog
CustomLog "|/home/proxy/logger.php" proxylog

the weird thing is that i do recieve some log input that is written, but only very rarely. far less than in the default log file. i also cant recognise a pattern.

i also waited long enough and successfully checked for missing log lines from a time frame before the last log entry in my custom piped log – to eliminiate the possibility that a buffer caused this. any ideas?

Best Answer

Your script doesn't exactly match what is expected for an Apache piped log script so what exactly is output is likely undefined. A minimal PHP example is:

#!/usr/local/bin/php

<?php
$stdin = fopen ('php://stdin', 'r');
ob_implicit_flush (true); // Use unbuffered output
while ($line = fgets ($stdin))
{
    print $line;
}
?>

See http://www.sudleyplace.com/pipederrorlogs.html for more details. First get it working so it simply outputs everything it receives and then add features to it as needed.

Related Topic