如何在symfony2命令中grep verbosity输出?

时间:2016-06-01 11:44:22

标签: bash symfony grep command output

在我的app/config/config.yml中,我添加了monolog的控制台处理程序:

monolog:
    handlers:
        console:
            type: console

因此,当我传递详细标记-vvv时,命令会产生对monolog的调用的输出,例如:

./bin/console text:hello k0pernikus -vvv 
[2016-06-01 13:19:27] event.DEBUG: Notified event "console.command" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".  {"uid":"c1e943a"}
[2016-06-01 13:19:27] event.DEBUG: Notified event "console.command" to listener "Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand".  {"uid":"c1e943a"}
Ipsum lorem dolorem
Hello k0pernikus!
Ipsum lorem dolorem

现在我想通过

grep第一行
/bin/console text:hello k0pernikus -vvv | grep DebugHandlersListener::configure

但我也得到了第二个DEBUG:

[2016-06-01 13:21:17] event.DEBUG: Notified event "console.command" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".  {"uid":"9e84992"}
[2016-06-01 13:21:17] event.DEBUG: Notified event "console.command" to listener "Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand".  {"uid":"9e84992"}

我还注意到输出行为很奇怪,因为我也无法重定向它:

./bin/console text:hello k0pernikus -vvv > fnord  
[2016-06-01 13:22:13] event.DEBUG: Notified event "console.command" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".  {"uid":"f21ef6f"}
[2016-06-01 13:22:13] event.DEBUG: Notified event "console.command" to listener "Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand".  {"uid":"f21ef6f"}

cat fnord 
Ipsum lorem dolorem
Hello k0pernikus!
Ipsum lorem dolorem

这似乎是想要的行为,但我对如何使用详细程度输出感到有点困惑。我对所有线路都不感兴趣,就在其中一条线路上。

如何grep for one line?

我的命令:

<?php
namespace Kopernikus\ApiBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * PlainTextHelloWorldCommand
 **/
class PlainTextHelloWorldCommand extends Command
{
    /**
     *
     */
    protected function configure()
    {
        $this
            ->setName('text:hello')
            ->addArgument('reciever', InputArgument::REQUIRED, 'Who do you want to greet?');
    }


    /**
     * @param InputInterface  $input
     * @param OutputInterface $output
     * @return int|null|void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $reciever = $input->getArgument('reciever');
        $output->writeln("Ipsum lorem dolorem");
        $output->writeln("Hello {$reciever}!");
        $output->writeln("Ipsum lorem dolorem");
    }
}

1 个答案:

答案 0 :(得分:2)

found the solution

./bin/console text:hello k0pernikus -vvv  2>&1 >/dev/null | grep DebugHandlersListener::config
[2016-06-01 13:56:11] event.DEBUG: Notified event "console.command" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".  {"uid":"885c2f6"}

问题是日志信息没有写入标准输出,而是写入错误流。