如何阻止 PHP 输出缓冲吃掉错误消息?
好吧,现在我已经深入了解了一点,我意识到这是一个愚蠢的问题,而且是错误的。事实证明,我维护的遗留代码的作者使用 php_init
语句将错误日志劫持到另一个文件。劫持发生在输出缓冲打开的同时,使得它看起来好像输出缓冲正在丢弃我的错误消息。
所以,版主先生,请您删除这个吧。感谢那些善意回答的人。
给定以下 PHP 脚本:
<?php
error_log('test');
ob_start();
error_log('test2');
ob_end_flush();
?>
我得到以下错误日志输出:
[04-Feb-2010 11:30:38] test
为什么输出缓冲会吃掉我的错误消息?我怎样才能让它停止?
或者,是否有另一种方法可以将消息从输出缓冲区中走私出来,或者它只是一个黑洞?
(使用 PHP 5.2.4-2ubuntu5.10
)
Well, now that I've gotten a bit further into it, I realize that this is a stupid question, and wrong. Turns out that the author of the legacy code I maintain was hi-jacking the error log to a different file with a php_init
statement. The hi-jacking occurred at the same time as the output buffering was turned on, making it appear as though output buffering was throwing away my error messages.
So, Mr. Moderator, feel free to delete this. Thanks to those who answered in good faith.
Given the following PHP script:
<?php
error_log('test');
ob_start();
error_log('test2');
ob_end_flush();
?>
I get the following error log output:
[04-Feb-2010 11:30:38] test
Why is output buffering eating my error messages? How can I make it stop?
Alternately, is there another way to smuggle messages out of an output buffer, or is it simply a black hole?
(Using PHP 5.2.4-2ubuntu5.10
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
异常会穿透 ob_start() 屏蔽
如果您想停止 PHP 脚本执行,不如抛出一个 异常,这会穿透 ob_start()、ob_end_flush() 屏蔽
考虑创建一个 Logger 类
不要使用您的方法直接输出,而不是使用包含日志的类或
holder
(或您的情况下的error
方法),例如:这样您就可以依赖于持有者对象,它不会丢弃消息并获取您想要显示的所有错误
Exceptions penetrate ob_start() shield
If you want to stop PHP script execution, rather throw an Exception, which will penetrate the ob_start(), ob_end_flush() shield
Consider creating a Logger class
Don't output directly with your method, rather use a class or a
holder
which incorporates the log (orerror
method in your case), eg:this way you can rely on the holder object, it will not discard messages and get all errors, that you wanted to display
我在实践中从未这样做过,但这应该可行:
您必须围绕 error_log() 构建一个包装器,该包装器
I've never done this in practice, but this should work:
You would have to build a wrapper around error_log() that