| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Console\Logger;
- use Psr\Log\AbstractLogger;
- use Psr\Log\InvalidArgumentException;
- use Psr\Log\LogLevel;
- use Symfony\Component\Console\Output\ConsoleOutputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- /**
- * PSR-3 compliant console logger.
- *
- * @author Kévin Dunglas <dunglas@gmail.com>
- *
- * @see https://www.php-fig.org/psr/psr-3/
- */
- class ConsoleLogger extends AbstractLogger
- {
- public const INFO = 'info';
- public const ERROR = 'error';
- /**
- * @var \Symfony\Component\Console\Output\OutputInterface
- */
- private $output;
- /**
- * @var mixed[]
- */
- private $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,
- ];
- /**
- * @var mixed[]
- */
- private $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,
- ];
- /**
- * @var bool
- */
- private $errored = false;
- /**
- * @param \Symfony\Component\Console\Output\OutputInterface $output
- * @param mixed[] $verbosityLevelMap
- * @param mixed[] $formatLevelMap
- */
- public function __construct($output, $verbosityLevelMap = [], $formatLevelMap = [])
- {
- $this->output = $output;
- $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
- $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
- }
- /**
- * @param mixed[] $context
- */
- public function log($level, $message, $context = [])
- {
- 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()
- {
- return $this->errored;
- }
- /**
- * Interpolates context values into the message placeholders.
- *
- * @author PHP Framework Interoperability Group
- * @param string $message
- * @param mixed[] $context
- */
- private function interpolate($message, $context)
- {
- if (strpos($message, '{') === false) {
- 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 '.get_class($val).']';
- } else {
- $replacements["{{$key}}"] = '['.\gettype($val).']';
- }
- }
- return strtr($message, $replacements);
- }
- }
|