I'm asynchronously posting to a PHP file that开发者_开发问答 echoes out a few key things. I want to write all of its output to a logfile. What would be the simplest way to do this?
I would use a simple wrapper, like http://www.redips.net/php/write-to-log-file/. All you need to do is include the file, instantiate the Logger class, and set a path. You would need to perform the logging operation after/before each echo.
<?php
// Before you have any output!
ob_start();
// All of your other code, echos, etc.
// Sends the Output Buffer, also captures it in the $output variables
$output = ob_get_flush();
// Some extra info for the Logfile, so you know when and who saw it
$logPrefix = "\n\n".
"Time: ".date( 'Y-m-d H:i:s' )."\n".
"IP: ".$_SERVER['REMOTE_ADDR']."\n\n";
// Write the data to the Logfile, and append it to the end (if file already exists)
file_put_contents( 'yourLogfile.txt' , $logPrefix.$output , FILE_APPEND );
?>
精彩评论