将回声漏斗到输出文件中


Funneling echoes into an output file

我正在异步发布到一个PHP文件中,该文件回显了一些关键的东西。我想将其所有输出写入日志文件。最简单的方法是什么?

我会使用一个简单的包装器,比如 http://www.redips.net/php/write-to-log-file/。您需要做的就是包含文件,实例化 Logger 类,并设置路径。您需要在每个回显之后/之前执行日志记录操作。

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