如何在 Symfony 中使用 Monolog 断言一行被记录

How to assert a line is logged using Monolog inside Symfony

我在 Symfony2 中使用 Monolog,使用默认的 MonologBu​​ndle。我试图在我的测试中断言,记录了一行。我已经在 config_test.yml:

中配置了这个
monolog:
    handlers:
        main:
            type:   test
            level:  debug

如何在我的测试中获得 Monolog TestHandler 的结果(继承自 Symfony2 的 WebTestCase)?

作为解决方案:

monolog 服务和搜索测试处理程序中获取所有处理程序。

foreach ($this->container->get('monolog')->getHandlers() as $handler) {
  if ($handler instanceof TestHandler) {
    $testHandler = $handler;
    break;
  }
}

if (!$testHandler) {
  throw new \RuntimeException('Oops, not exist "test" handler in monolog.');
}

$this->assertFalse($testHandler->hasCritical()); // Or another assertions

在您的命令 class 中,您只需将处理程序设置为 pushHandler():

namespace AppBundle\Command;

use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class YourCommand extends ContainerAwareCommand
{
    // ...

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $logger = $this->getContainer()->get('logger');

        // PUSH THE OutputInterface OBJECT INTO MONOLOG
        $logger->pushHandler(new ConsoleHandler($output));

        // Your command logic here...
    }

在你的测试中,使用 CommandTester:

namespace AppBundle\Tests\Command;

use AppBundle\Command\YourCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;

class YourCommandTest extends KernelTestCase
{
    public function testExecute()
    {
        $kernel = $this->createKernel();
        $kernel->boot();

        // mock the Kernel or create one depending on your needs
        $application = new Application($kernel);
        $application->add(new YourCommand());

        $command = $application->find('acme:your:command');

        $commandTester = new CommandTester($command);
        $commandTester->execute(
            array('command'   => $command->getName()),
            /**
             * Here set the verbosity
             */
            array('verbosity' => OutputInterface::VERBOSITY_DEBUG)
        );

        // die(print_r($commandTester->getDisplay()));

        $this->assertRegExp('/.../', $commandTester->getDisplay());
    }
}

关注array('verbosity' => OutputInterface::VERBOSITY_DEBUG).

通过这种方式您可以获得所有日志(在本例中为 INFO,设置为 $logger->info('Starting <info>acme:your:command</info>');):

[2015-08-13 23:39:22] app.INFO: Starting acme:your:command: 

现在您可以使用$this->assertRegExp()检查特定行是否已记录。

您还可以将 string 转换为 array

explode('\n', $commandTester->getDisplay())

此解决方案是 found here and is explained in the documentation of Monolog here

更多关于 Monolog and Symfony (Symfony Docu)

更多关于 Monolog Handlers (Monolog Docu)

Symfony 5(自动装配),PHP7.4

namespace App\Command;

use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class YourCommand extends Command
{
    protected static $defaultName = 'acme:your:command';
    private LoggerInterface $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // PUSH THE OutputInterface OBJECT INTO MONOLOG
        if ($this->logger instanceof Logger) {
            $this->logger->pushHandler(new ConsoleHandler($output));
        }

        // Your command logic here...

        return self::SUCCESS;
    }
}

在你的测试中,使用 CommandTester:

namespace AppBundle\Tests\Command;

use AppBundle\Command\YourCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;

class YourCommandTest extends KernelTestCase
{
    public function testExecute()
    {
        $kernel = static::createKernel();
        $application = new Application($kernel);

        $command = $application->find('acme:your:command');
        $commandTester = new CommandTester($command);
        $commandTester->execute(
            ['command'   => $command->getName()],
            /**
             * Here set the verbosity
             */
            ['verbosity' => OutputInterface::VERBOSITY_DEBUG]
        );

        $output = $commandTester->getDisplay();
        // die(print_r($commandTester->getDisplay()));

        self::assertStringContainsString('/.../', $$output);
    }
}