Skip to content

[MonologBridge] fixed Console handler priorities #11448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public function onTerminate(ConsoleTerminateEvent $event)
public static function getSubscribedEvents()
{
return array(
ConsoleEvents::COMMAND => 'onCommand',
ConsoleEvents::TERMINATE => 'onTerminate'
ConsoleEvents::COMMAND => array('onCommand', 255),
ConsoleEvents::TERMINATE => array('onTerminate', -255),
);
}

Expand Down
44 changes: 44 additions & 0 deletions src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@

use Monolog\Logger;
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Console\Command\Command;

/**
* Tests the ConsoleHandler and also the ConsoleFormatter.
Expand Down Expand Up @@ -156,4 +162,42 @@ public function testWritingAndFormatting()

$this->assertTrue($handler->handle($errorRecord), 'The handler finished handling the log as bubble is false.');
}

public function testLogsFromListeners()
{
$output = new BufferedOutput();
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);

$handler = new ConsoleHandler(null, false);

$logger = new Logger('app');
$logger->pushHandler($handler);

$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::COMMAND, function () use ($logger) {
$logger->addInfo('Before command message.');
});
$dispatcher->addListener(ConsoleEvents::TERMINATE, function () use ($logger) {
$logger->addInfo('Before terminate message.');
});

$dispatcher->addSubscriber($handler);

$dispatcher->addListener(ConsoleEvents::COMMAND, function () use ($logger) {
$logger->addInfo('After command message.');
});
$dispatcher->addListener(ConsoleEvents::TERMINATE, function () use ($logger) {
$logger->addInfo('After terminate message.');
});

$event = new ConsoleCommandEvent(new Command('foo'), $this->getMock('Symfony\Component\Console\Input\InputInterface'), $output);
$dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
$this->assertContains('Before command message.', $out = $output->fetch());
$this->assertContains('After command message.', $out);

$event = new ConsoleTerminateEvent(new Command('foo'), $this->getMock('Symfony\Component\Console\Input\InputInterface'), $output, 0);
$dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
$this->assertContains('Before terminate message.', $out = $output->fetch());
$this->assertContains('After terminate message.', $out);
}
}