2016-04-22 56 views
0

我有一个类在我的控制台中执行命令。这个类是由@logger.handler定义的LoggerInterface定义的。PHP:将bash命令的输出重定向到默认记录器

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

  • 我不想显示导致默认流
  • 我希望把结果在默认的日志文件($记录器定义),例如应用程序/日志/ application.log

这个类的主要方法是这样的:

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

我加> 2>&1不显示任何在当前流,因为我不知道怎么弄app/log/application.log从$输出...

问题是我没有在我的app/config/application.log!

你知道我错过了什么吗?

+0

有你尝试 '$ command = $ command。 “> \”app/config/application.log \“”'; ? – Tarek

+0

@Tarek我很肯定它可以工作,但我不想设置这个硬编码路径,因为当它在开发模式下运行你的应用程序或者依赖注入被忽略时,它可能会有所不同。 – pierallard

+0

然后'$ command = $命令。 “> \”“。$ logger。”\“”'? – Tarek

回答

0

你是不是在输出因> 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); 
+0

此解决方案的工作原理,但只返回执行命令的最后一行......并非如预期的那样:( – pierallard

+0

更新了我的答案:) – Tarek

+0

此解决方案在运行时在控制台中显示所有结果。 – pierallard