Skip to content

Commit 640aa2d

Browse files
committed
allow optional ConsoleOutput directly in the constructor
1 parent b6538f6 commit 640aa2d

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

Handler/ConsoleHandler.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* Writes logs to the console output depending on its verbosity setting.
2626
*
2727
* 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.
28+
* Instead of listening to the console events, the output can also be set manually.
2929
*
3030
* The minimum logging level at which this handler will be triggered depends on the
3131
* verbosity setting of the console output. The default mapping is:
@@ -58,13 +58,16 @@ class ConsoleHandler extends AbstractProcessingHandler implements EventSubscribe
5858
/**
5959
* Constructor.
6060
*
61-
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack
62-
* @param array $verbosityLevelMap Array that maps the OutputInterface verbosity to a minimum logging level
63-
* (leave empty to use the default mapping)
61+
* @param OutputInterface|null $output The console output to use (the handler remains disabled when passing null
62+
* until the output is set, e.g. by using console events)
63+
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack
64+
* @param array $verbosityLevelMap Array that maps the OutputInterface verbosity to a minimum logging
65+
* level (leave empty to use the default mapping)
6466
*/
65-
public function __construct($bubble = true, array $verbosityLevelMap = array())
67+
public function __construct(OutputInterface $output = null, $bubble = true, array $verbosityLevelMap = array())
6668
{
6769
parent::__construct(Logger::DEBUG, $bubble);
70+
$this->output = $output;
6871

6972
if ($verbosityLevelMap) {
7073
$this->verbosityLevelMap = $verbosityLevelMap;

Tests/Handler/ConsoleHandlerTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ConsoleHandlerTest extends TestCase
2525
{
2626
public function testConstructor()
2727
{
28-
$handler = new ConsoleHandler(false);
28+
$handler = new ConsoleHandler(null, false);
2929
$this->assertFalse($handler->getBubble(), 'the bubble parameter gets propagated');
3030
}
3131

@@ -46,8 +46,7 @@ public function testVerbosityMapping($verbosity, $level, $isHandling)
4646
->method('getVerbosity')
4747
->will($this->returnValue($verbosity))
4848
;
49-
$handler = new ConsoleHandler();
50-
$handler->setOutput($output);
49+
$handler = new ConsoleHandler($output);
5150
$this->assertSame($isHandling, $handler->isHandling(array('level' => $level)),
5251
'->isHandling returns correct value depending on console verbosity and log level'
5352
);
@@ -81,8 +80,7 @@ public function testVerbosityChanged()
8180
->method('getVerbosity')
8281
->will($this->returnValue(OutputInterface::VERBOSITY_DEBUG))
8382
;
84-
$handler = new ConsoleHandler();
85-
$handler->setOutput($output);
83+
$handler = new ConsoleHandler($output);
8684
$this->assertFalse($handler->isHandling(array('level' => Logger::NOTICE)),
8785
'when verbosity is set to quiet, the handler does not handle the log'
8886
);
@@ -126,7 +124,7 @@ public function testWritingAndFormatting()
126124
->will($this->returnValue($errorOutput))
127125
;
128126

129-
$handler = new ConsoleHandler(false);
127+
$handler = new ConsoleHandler(null, false);
130128
$handler->setOutput($output);
131129

132130
$infoRecord = array(

0 commit comments

Comments
 (0)