我正在使用 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???);
}
在我想要的文件中:
[Output from the calls to other services, if any]
The operation was successful.
你能帮帮我吗?
我试过这样的:
$stream = $output->getStream();
$content = stream_get_contents($stream, 5);
但命令不会以这种方式完成。 :(
最佳答案
您可以通过 php app/console your:command > output.log
使用标准 shell 方法转发命令输出。或者,如果这不是一个选项,您可以为 OutputInterface
引入一个包装器,它将写入一个流,然后将调用转发到包装的输出。
关于php - 获取 Symfony 命令的输出并将其保存到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26231935/