如何阻止 PHP 输出缓冲吃掉错误消息?

发布于 2024-08-19 22:51:16 字数 531 浏览 3 评论 0原文

好吧,现在我已经深入了解了一点,我意识到这是一个愚蠢的问题,而且是错误的。事实证明,我维护的遗留代码的作者使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蹲在坟头点根烟 2024-08-26 22:51:16

异常会穿透 ob_start() 屏蔽

如果您想停止 PHP 脚本执行,不如抛出一个 异常,这会穿透 ob_start()、ob_end_flush() 屏蔽

echo 'before output buffer';
ob_start();
throw new Exception('this will be seen');
ob_end_flush();

考虑创建一个 Logger 类

不要使用您的方法直接输出,而不是使用包含日志的类或holder(或您的情况下的error方法),例如:

class Logger
{
    private $_messages = array();

    public function __construct()
    {
        $this->_messages['errors'] = array();
        $this->_messages['debug'] = array();
    }

    public function error($msg)
    {
        $this->_messages['errors'][] = $msg;
    }

    public function debug($msg)
    {
        $this->_messages['debug'] = $msg;
    }

    public function getErrors()
    {
        return $this->_messages['errors'];
    }

}

$logger = new Logger();

$logger->error('error1');

ob_start();

$logger->error('error2');

ob_end_flush();

print_r($logger->getErrors());

这样您就可以依赖于持有者对象,它不会丢弃消息并获取您想要显示的所有错误

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

echo 'before output buffer';
ob_start();
throw new Exception('this will be seen');
ob_end_flush();

Consider creating a Logger class

Don't output directly with your method, rather use a class or a holder which incorporates the log (or error method in your case), eg:

class Logger
{
    private $_messages = array();

    public function __construct()
    {
        $this->_messages['errors'] = array();
        $this->_messages['debug'] = array();
    }

    public function error($msg)
    {
        $this->_messages['errors'][] = $msg;
    }

    public function debug($msg)
    {
        $this->_messages['debug'] = $msg;
    }

    public function getErrors()
    {
        return $this->_messages['errors'];
    }

}

$logger = new Logger();

$logger->error('error1');

ob_start();

$logger->error('error2');

ob_end_flush();

print_r($logger->getErrors());

this way you can rely on the holder object, it will not discard messages and get all errors, that you wanted to display

旧人 2024-08-26 22:51:16

我在实践中从未这样做过,但这应该可行:

您必须围绕 error_log() 构建一个包装器,该包装器

  1. 使用 ob_get_contents() 存储正在缓冲的输出,
  2. 使用 ob_clean() 擦除输出缓冲区,
  3. 写出错误消息并ob_flush()es 它
  4. 使用 echo() 写回存储的输出

I've never done this in practice, but this should work:

You would have to build a wrapper around error_log() that

  1. stores the output you are buffering using ob_get_contents()
  2. erases the output buffer using ob_clean()
  3. writes out the error message and ob_flush()es it
  4. writes back the stored output using echo()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文