Skip to content

Commit 3156bdf

Browse files
committed
merge handler and listener
1 parent 01f30ba commit 3156bdf

File tree

4 files changed

+40
-92
lines changed

4 files changed

+40
-92
lines changed

DependencyInjection/MonologExtension.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
class MonologExtension extends Extension
2929
{
3030
private $nestedHandlers = array();
31-
private $consoleHandlers = array();
3231

3332
/**
3433
* Loads the Monolog configuration.
@@ -55,12 +54,6 @@ public function load(array $configs, ContainerBuilder $container)
5554
);
5655
}
5756

58-
if ($this->consoleHandlers) {
59-
$consoleListener = $container->getDefinition('monolog.listener.console');
60-
$consoleListener->replaceArgument(0, $this->consoleHandlers);
61-
$consoleListener->setAbstract(false);
62-
}
63-
6457
ksort($handlers);
6558
$sortedHandlers = array();
6659
foreach ($handlers as $priorityHandlers) {
@@ -134,7 +127,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
134127
$definition->setArguments(array(
135128
$handler['bubble'],
136129
));
137-
$this->consoleHandlers[] = new Reference($handlerId);
130+
$definition->addTag('kernel.event_subscriber');
138131
break;
139132

140133
case 'firephp':

EventListener/LogToConsoleListener.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

Handler/ConsoleHandler.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,22 @@
1414
use Monolog\Handler\AbstractProcessingHandler;
1515
use Monolog\Logger;
1616
use Symfony\Bundle\MonologBundle\Formatter\ConsoleFormatter;
17+
use Symfony\Component\Console\ConsoleEvents;
18+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
19+
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
1720
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1821
use Symfony\Component\Console\Output\OutputInterface;
22+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1923

2024
/**
2125
* Writes to the console output depending on its verbosity setting.
2226
*
23-
* It is disabled by default and gets activated when setOutput is called.
27+
* It is disabled by default and gets activated as soon as a command is executed.
28+
* Instead of listening to the console events, setOutput can also be called manually.
2429
*
2530
* @author Tobias Schultze <http://tobion.de>
2631
*/
27-
class ConsoleHandler extends AbstractProcessingHandler
32+
class ConsoleHandler extends AbstractProcessingHandler implements EventSubscriberInterface
2833
{
2934
/**
3035
* @var OutputInterface|null
@@ -106,6 +111,38 @@ public function close()
106111
parent::close();
107112
}
108113

114+
/**
115+
* Before a command is executed, the handler gets activated and the console output
116+
* is set in order to know where to write the logs.
117+
*
118+
* @param ConsoleCommandEvent $event
119+
*/
120+
public function onCommand(ConsoleCommandEvent $event)
121+
{
122+
$this->setOutput($event->getOutput());
123+
}
124+
125+
/**
126+
* After a command has been executed, it disables the output.
127+
*
128+
* @param ConsoleTerminateEvent $event
129+
*/
130+
public function onTerminate(ConsoleTerminateEvent $event)
131+
{
132+
$this->close();
133+
}
134+
135+
/**
136+
* {@inheritdoc}
137+
*/
138+
public static function getSubscribedEvents()
139+
{
140+
return array(
141+
ConsoleEvents::COMMAND => 'onCommand',
142+
ConsoleEvents::TERMINATE => 'onTerminate'
143+
);
144+
}
145+
109146
/**
110147
* {@inheritdoc}
111148
*/

Resources/config/monolog.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,5 @@
4040
<service id="monolog.logger_prototype" class="%monolog.logger.class%" abstract="true">
4141
<argument /><!-- Channel -->
4242
</service>
43-
44-
<!-- The MonologExtension removes the abstract flag when handlers of type "console" are used and inserts them -->
45-
<service id="monolog.listener.console" class="%monolog.listener.console.class%" abstract="true">
46-
<tag name="kernel.event_subscriber" />
47-
<argument type="collection" /><!-- Array of ConsoleHandler instances -->
48-
</service>
4943
</services>
5044
</container>

0 commit comments

Comments
 (0)