Skip to content

Commit cfc578a

Browse files
[Console] Add ConsoleLogger::hasErrored()
1 parent 22f7ed7 commit cfc578a

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/Symfony/Component/Console/Logger/ConsoleLogger.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class ConsoleLogger extends AbstractLogger
5959
LogLevel::INFO => self::INFO,
6060
LogLevel::DEBUG => self::INFO,
6161
);
62+
private $errored = false;
6263

6364
/**
6465
* @param OutputInterface $output
@@ -81,11 +82,14 @@ public function log($level, $message, array $context = array())
8182
throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
8283
}
8384

85+
$output = $this->output;
86+
8487
// Write to the error output if necessary and available
85-
if ($this->formatLevelMap[$level] === self::ERROR && $this->output instanceof ConsoleOutputInterface) {
86-
$output = $this->output->getErrorOutput();
87-
} else {
88-
$output = $this->output;
88+
if ($this->formatLevelMap[$level] === self::ERROR) {
89+
if ($this->output instanceof ConsoleOutputInterface) {
90+
$output = $output->getErrorOutput();
91+
}
92+
$this->errored = true;
8993
}
9094

9195
// the if condition check isn't necessary -- it's the same one that $output will do internally anyway.
@@ -95,6 +99,14 @@ public function log($level, $message, array $context = array())
9599
}
96100
}
97101

102+
/**
103+
* Returns true when any messages have been logged at error levels.
104+
*/
105+
public function hasErrored()
106+
{
107+
return $this->errored;
108+
}
109+
98110
/**
99111
* Interpolates context values into the message placeholders.
100112
*

src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,17 @@ public function provideOutputMappingParams()
8888
array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, true, $quietMap),
8989
);
9090
}
91+
92+
public function testHasErrored()
93+
{
94+
$logger = new ConsoleLogger(new BufferedOutput());
95+
96+
$this->assertFalse($logger->hasErrored());
97+
98+
$logger->warning('foo');
99+
$this->assertFalse($logger->hasErrored());
100+
101+
$logger->error('bar');
102+
$this->assertTrue($logger->hasErrored());
103+
}
91104
}

0 commit comments

Comments
 (0)