PHP:将 bash 命令的输出重定向到默认记录器中


PHP: Redirect output of a bash command into default logger

我有一个类在我的控制台中执行命令。此类使用从 @logger.handler 定义的LoggerInterface定义。

我想在我的 bash 中执行命令,但是:

  • 我不想在默认流中显示结果
  • 我想将结果放在默认日志文件(由$logger定义)中,例如.app/logs/application.log

此类的主要方法如下所示:

$command = $command . " > 2>&1";
$returnVal = null;
$output = [];
exec($command, $output, $returnVal);
$this->logger->debug($output);

我添加了> 2>&1以在当前流中不显示任何内容,并且因为我不知道如何从$output中获取app/log/application.log......

问题是我的应用程序/配置/应用程序中没有任何内容.log!

你知道我错过了什么吗?

由于> 2>&1,您没有捕获输出中的任何内容

//$command = $command . " > 2>&1";
$returnVal = null;
$output = [];
exec($command, $output, $returnVal);
for ($i=0;$i<count($output);$i++)
    $this->logger->debug($output[$i]);

如果调试器为每个时间戳创建多个时间戳,并且它不是所需的输出,请使用以下命令:

$returnVal = null;
$output = [];
exec($command, $output, $returnVal);
$result=""
for ($i=0;$i<count($output);$i++)
    $result.=$output[i]."'n";
$this->logger->debug($result);