maj+ header
This commit is contained in:
+36
-12
@@ -14,8 +14,8 @@ namespace Symfony\Component\Console\Logger;
|
||||
use Psr\Log\AbstractLogger;
|
||||
use Psr\Log\InvalidArgumentException;
|
||||
use Psr\Log\LogLevel;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* PSR-3 compliant console logger.
|
||||
@@ -50,6 +50,7 @@ class ConsoleLogger extends AbstractLogger
|
||||
LogLevel::INFO => self::INFO,
|
||||
LogLevel::DEBUG => self::INFO,
|
||||
);
|
||||
private $errored = false;
|
||||
|
||||
public function __construct(OutputInterface $output, array $verbosityLevelMap = array(), array $formatLevelMap = array())
|
||||
{
|
||||
@@ -67,18 +68,33 @@ class ConsoleLogger extends AbstractLogger
|
||||
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 (self::ERROR === $this->formatLevelMap[$level] && $this->output instanceof ConsoleOutputInterface) {
|
||||
$output = $this->output->getErrorOutput();
|
||||
} else {
|
||||
$output = $this->output;
|
||||
if (self::ERROR === $this->formatLevelMap[$level]) {
|
||||
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.
|
||||
// We only do it for efficiency here as the message formatting is relatively expensive.
|
||||
if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) {
|
||||
$output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)));
|
||||
$output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when any messages have been logged at error levels.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasErrored()
|
||||
{
|
||||
return $this->errored;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interpolates context values into the message placeholders.
|
||||
*
|
||||
@@ -91,15 +107,23 @@ class ConsoleLogger extends AbstractLogger
|
||||
*/
|
||||
private function interpolate($message, array $context)
|
||||
{
|
||||
// build a replacement array with braces around the context keys
|
||||
$replace = array();
|
||||
if (false === strpos($message, '{')) {
|
||||
return $message;
|
||||
}
|
||||
|
||||
$replacements = array();
|
||||
foreach ($context as $key => $val) {
|
||||
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
|
||||
$replace[sprintf('{%s}', $key)] = $val;
|
||||
if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
|
||||
$replacements["{{$key}}"] = $val;
|
||||
} elseif ($val instanceof \DateTimeInterface) {
|
||||
$replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
|
||||
} elseif (\is_object($val)) {
|
||||
$replacements["{{$key}}"] = '[object '.\get_class($val).']';
|
||||
} else {
|
||||
$replacements["{{$key}}"] = '['.\gettype($val).']';
|
||||
}
|
||||
}
|
||||
|
||||
// interpolate replacement values into the message and return
|
||||
return strtr($message, $replace);
|
||||
return strtr($message, $replacements);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user