Skip to content

[Console] Add ConsoleLogger::hasErrored() #19090

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
Jun 20, 2016
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
20 changes: 16 additions & 4 deletions src/Symfony/Component/Console/Logger/ConsoleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ConsoleLogger extends AbstractLogger
LogLevel::INFO => self::INFO,
LogLevel::DEBUG => self::INFO,
);
private $errored = false;

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

$output = $this->output;

// Write to the error output if necessary and available
if ($this->formatLevelMap[$level] === self::ERROR && $this->output instanceof ConsoleOutputInterface) {
$output = $this->output->getErrorOutput();
} else {
$output = $this->output;
if ($this->formatLevelMap[$level] === self::ERROR) {
if ($this->output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
}
$this->errored = true;
}

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

/**
* Returns true when any messages have been logged at error levels.
*/
public function hasErrored()
{
return $this->errored;
}

/**
* Interpolates context values into the message placeholders.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,17 @@ public function provideOutputMappingParams()
array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, true, $quietMap),
);
}

public function testHasErrored()
{
$logger = new ConsoleLogger(new BufferedOutput());

$this->assertFalse($logger->hasErrored());

$logger->warning('foo');
$this->assertFalse($logger->hasErrored());

$logger->error('bar');
$this->assertTrue($logger->hasErrored());
}
}