【问题标题】:Where to change the output of the Symfony2 ConsoleOutput?在哪里更改 Symfony2 ConsoleOutput 的输出?
【发布时间】:2015-07-03 10:57:13
【问题描述】:

我正在通过ConsoleOuput 打印一些行。我喜欢使用 Table Helper,但它默认使用 StdOut,这似乎是硬编码的,就像在 \Symfony/Component/Console/Output/ConsoleOutput.php 中一样,它说:

 * This class is a convenient wrapper around `StreamOutput`.
 *
 *     $output = new ConsoleOutput();
 *
 * This is equivalent to:
 *
 *     $output = new StreamOutput(fopen('php://stdout', 'w'));

在项目中,我们有 Log4Php,并通过将默认输出从 stdout 更改为自定义输出将其存储到文件中。

有没有办法将 ConsoleOutput 的输出更改为 sdtout 以外的其他内容?

因为到目前为止,我可以在StdOut中看到命令行中的输出,但是当php4log输出重定向到文件时,ConsoleOutput仍然会在StdOut中。

如何将这些输出合并在一起?

【问题讨论】:

    标签: php symfony logging console-application


    【解决方案1】:

    使用BufferedOutput() 而不是ConsoleOutput()。它将存储消息,然后您可以在任何您想要的地方使用这些消息。

    使用$output->fetch() 方法以字符串形式获取消息。该字符串包含您之前对OutputInterface 所做的所有写入操作,并且方法调用也会清空缓冲区。

    如果您需要在不同的记录器中使用它,在您的情况下是 log4php 实例,它看起来像:

    $this->getLog4PhpLogger()
        ->info(
            $this->output->fetch()
        );
    

    【讨论】:

      猜你喜欢
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多