class ConsoleLogger
PSR-3 compliant console logger.
@author Kévin Dunglas <dunglas@gmail.com>
Hierarchy
- class \Psr\Log\AbstractLogger implements \Psr\Log\LoggerInterface uses \Psr\Log\LoggerTrait
- class \Symfony\Component\Console\Logger\ConsoleLogger extends \Psr\Log\AbstractLogger
Expanded class hierarchy of ConsoleLogger
See also
https://www.php-fig.org/psr/psr-3/
1 file declares its use of ConsoleLogger
- RecipeCommand.php in core/
lib/ Drupal/ Core/ Recipe/ RecipeCommand.php
File
-
vendor/
symfony/ console/ Logger/ ConsoleLogger.php, line 27
Namespace
Symfony\Component\Console\LoggerView source
class ConsoleLogger extends AbstractLogger {
public const INFO = 'info';
public const ERROR = 'error';
private array $verbosityLevelMap = [
LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE,
LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE,
LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG,
];
private array $formatLevelMap = [
LogLevel::EMERGENCY => self::ERROR,
LogLevel::ALERT => self::ERROR,
LogLevel::CRITICAL => self::ERROR,
LogLevel::ERROR => self::ERROR,
LogLevel::WARNING => self::INFO,
LogLevel::NOTICE => self::INFO,
LogLevel::INFO => self::INFO,
LogLevel::DEBUG => self::INFO,
];
private bool $errored = false;
public function __construct(OutputInterface $output, array $verbosityLevelMap = [], array $formatLevelMap = []) {
$this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
$this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
}
public function log($level, $message, array $context = []) : void {
if (!isset($this->verbosityLevelMap[$level])) {
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]) {
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)), $this->verbosityLevelMap[$level]);
}
}
/**
* Returns true when any messages have been logged at error levels.
*/
public function hasErrored() : bool {
return $this->errored;
}
/**
* Interpolates context values into the message placeholders.
*
* @author PHP Framework Interoperability Group
*/
private function interpolate(string $message, array $context) : string {
if (!str_contains($message, '{')) {
return $message;
}
$replacements = [];
foreach ($context as $key => $val) {
if (null === $val || \is_scalar($val) || $val instanceof \Stringable) {
$replacements["{{$key}}"] = $val;
}
elseif ($val instanceof \DateTimeInterface) {
$replacements["{{$key}}"] = $val->format(\DateTimeInterface::RFC3339);
}
elseif (\is_object($val)) {
$replacements["{{$key}}"] = '[object ' . $val::class . ']';
}
else {
$replacements["{{$key}}"] = '[' . \gettype($val) . ']';
}
}
return strtr($message, $replacements);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
ConsoleLogger::$errored | private | property | |
ConsoleLogger::$formatLevelMap | private | property | |
ConsoleLogger::$verbosityLevelMap | private | property | |
ConsoleLogger::ERROR | public | constant | |
ConsoleLogger::hasErrored | public | function | Returns true when any messages have been logged at error levels. |
ConsoleLogger::INFO | public | constant | |
ConsoleLogger::interpolate | private | function | Interpolates context values into the message placeholders. |
ConsoleLogger::log | public | function | |
ConsoleLogger::__construct | public | function |