ConsoleLogger.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Logger;
  11. use Psr\Log\AbstractLogger;
  12. use Psr\Log\InvalidArgumentException;
  13. use Psr\Log\LogLevel;
  14. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. /**
  17. * PSR-3 compliant console logger.
  18. *
  19. * @author Kévin Dunglas <dunglas@gmail.com>
  20. *
  21. * @see https://www.php-fig.org/psr/psr-3/
  22. */
  23. class ConsoleLogger extends AbstractLogger
  24. {
  25. public const INFO = 'info';
  26. public const ERROR = 'error';
  27. /**
  28. * @var \Symfony\Component\Console\Output\OutputInterface
  29. */
  30. private $output;
  31. /**
  32. * @var mixed[]
  33. */
  34. private $verbosityLevelMap = [
  35. LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
  36. LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
  37. LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
  38. LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
  39. LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
  40. LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE,
  41. LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE,
  42. LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG,
  43. ];
  44. /**
  45. * @var mixed[]
  46. */
  47. private $formatLevelMap = [
  48. LogLevel::EMERGENCY => self::ERROR,
  49. LogLevel::ALERT => self::ERROR,
  50. LogLevel::CRITICAL => self::ERROR,
  51. LogLevel::ERROR => self::ERROR,
  52. LogLevel::WARNING => self::INFO,
  53. LogLevel::NOTICE => self::INFO,
  54. LogLevel::INFO => self::INFO,
  55. LogLevel::DEBUG => self::INFO,
  56. ];
  57. /**
  58. * @var bool
  59. */
  60. private $errored = false;
  61. /**
  62. * @param \Symfony\Component\Console\Output\OutputInterface $output
  63. * @param mixed[] $verbosityLevelMap
  64. * @param mixed[] $formatLevelMap
  65. */
  66. public function __construct($output, $verbosityLevelMap = [], $formatLevelMap = [])
  67. {
  68. $this->output = $output;
  69. $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
  70. $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
  71. }
  72. /**
  73. * @param mixed[] $context
  74. */
  75. public function log($level, $message, $context = [])
  76. {
  77. if (!isset($this->verbosityLevelMap[$level])) {
  78. throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
  79. }
  80. $output = $this->output;
  81. // Write to the error output if necessary and available
  82. if (self::ERROR === $this->formatLevelMap[$level]) {
  83. if ($this->output instanceof ConsoleOutputInterface) {
  84. $output = $output->getErrorOutput();
  85. }
  86. $this->errored = true;
  87. }
  88. // the if condition check isn't necessary -- it's the same one that $output will do internally anyway.
  89. // We only do it for efficiency here as the message formatting is relatively expensive.
  90. if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) {
  91. $output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]);
  92. }
  93. }
  94. /**
  95. * Returns true when any messages have been logged at error levels.
  96. */
  97. public function hasErrored()
  98. {
  99. return $this->errored;
  100. }
  101. /**
  102. * Interpolates context values into the message placeholders.
  103. *
  104. * @author PHP Framework Interoperability Group
  105. * @param string $message
  106. * @param mixed[] $context
  107. */
  108. private function interpolate($message, $context)
  109. {
  110. if (strpos($message, '{') === false) {
  111. return $message;
  112. }
  113. $replacements = [];
  114. foreach ($context as $key => $val) {
  115. if (null === $val || \is_scalar($val) || $val instanceof \Stringable) {
  116. $replacements["{{$key}}"] = $val;
  117. } elseif ($val instanceof \DateTimeInterface) {
  118. $replacements["{{$key}}"] = $val->format(\DateTimeInterface::RFC3339);
  119. } elseif (\is_object($val)) {
  120. $replacements["{{$key}}"] = '[object '.get_class($val).']';
  121. } else {
  122. $replacements["{{$key}}"] = '['.\gettype($val).']';
  123. }
  124. }
  125. return strtr($message, $replacements);
  126. }
  127. }