获取Symfony命令的输出并将其保存到文件中

Fae*_*ery 6 php command stream symfony

我正在使用Symfony 2.0.

我在Symfony中创建了一个命令,我想获取其输出并将其写入文件.

我想要的就是把所有写在标准输出上的东西(在控制台上)放在一个变量中.我指的是在命令中回显的东西,在其他文件中捕获的异常,由命令调用等等.我想在屏幕上和变量中输出(为了在文件中写入变量的内容).我将execute()在命令方法结束时在文件中写入.

像这样的东西:

protected function execute(InputInterface $input, OutputInterface $output)
{
    // some logic and calls to services and functions
    echo 'The operation was successful.';

    $this->writeLogToFile($file, $output???);
}
Run Code Online (Sandbox Code Playgroud)

在我想要的文件中:

[Output from the calls to other services, if any]
The operation was successful.
Run Code Online (Sandbox Code Playgroud)

你能帮我么?

我试过这样的事情:

   $stream  = $output->getStream();
   $content = stream_get_contents($stream, 5);
Run Code Online (Sandbox Code Playgroud)

但命令没有以这种方式完成.:(

kix*_*kix 10

您可以使用标准shell方法转发命令输出php app/console your:command > output.log.或者,如果这不是一个选项,您可以引入一个包装器来 OutputInterface写入流,然后将调用转发给包装输出.

  • 不幸的是,管道不起作用,因为 Symfony 似乎以不同的方式输出内容...... (2认同)