开发者

Funneling echoes into an output file

开发者 https://www.devze.com 2023-03-03 21:16 出处:网络
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 simpl

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 );
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消