我正在研究一个每天运行一次的cron php脚本.因为它以这种方式运行,所以无法看到文件的输出.
我可以将我想要的所有消息写入变量,不断追加我想要写入文件的信息,但这将非常繁琐,而且我没有必要预感.
是否有PHP命令告诉写缓冲区在某处写入日志文件?有没有办法访问已经发送到缓冲区的内容,以便我可以看到我的脚本发出的消息.
例如,让我们说脚本说
PHP:
<?
echo 'hello there';
echo 'hello world';
?>
Run Code Online (Sandbox Code Playgroud)
它应输出到一个文件说:'hello therehello world';
有任何想法吗?这可能吗?
我已经知道了
file_put_contents('log.txt', 'some data', FILE_APPEND);
Run Code Online (Sandbox Code Playgroud)
这取决于"某些数据",当我不知道"某些数据"是什么时,除非我把它放在一个变量中.我试图捕捉PHP输出的结果.
您可能希望将输出重定向到crontab:
php /path/to/php/file.php >> log.txt
Run Code Online (Sandbox Code Playgroud)
或者使用PHP,例如file_put_contents():
file_put_contents('log.txt', 'some data', FILE_APPEND);
Run Code Online (Sandbox Code Playgroud)
如果要捕获所有PHP输出,请使用ob_函数,如:
ob_start();
/*
We're doing stuff..
stuff
...
and again
*/
$content = ob_get_contents();
ob_end_clean(); //here, output is cleaned. You may want to flush it with ob_end_flush()
file_put_contents('log.txt', $content, FILE_APPEND);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3353 次 |
| 最近记录: |