ConsoleLogger.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 http://www.php-fig.org/psr/psr-3/
  22. */
  23. class ConsoleLogger extends AbstractLogger
  24. {
  25. const INFO = 'info';
  26. const ERROR = 'error';
  27. private $output;
  28. private $verbosityLevelMap = [
  29. LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
  30. LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
  31. LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
  32. LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
  33. LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
  34. LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE,
  35. LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE,
  36. LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG,
  37. ];
  38. private $formatLevelMap = [
  39. LogLevel::EMERGENCY => self::ERROR,
  40. LogLevel::ALERT => self::ERROR,
  41. LogLevel::CRITICAL => self::ERROR,
  42. LogLevel::ERROR => self::ERROR,
  43. LogLevel::WARNING => self::INFO,
  44. LogLevel::NOTICE => self::INFO,
  45. LogLevel::INFO => self::INFO,
  46. LogLevel::DEBUG => self::INFO,
  47. ];
  48. private $errored = false;
  49. public function __construct(OutputInterface $output, array $verbosityLevelMap = [], array $formatLevelMap = [])
  50. {
  51. $this->output = $output;
  52. $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
  53. $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function log($level, $message, array $context = [])
  59. {
  60. if (!isset($this->verbosityLevelMap[$level])) {
  61. throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
  62. }
  63. $output = $this->output;
  64. // Write to the error output if necessary and available
  65. if (self::ERROR === $this->formatLevelMap[$level]) {
  66. if ($this->output instanceof ConsoleOutputInterface) {
  67. $output = $output->getErrorOutput();
  68. }
  69. $this->errored = true;
  70. }
  71. // the if condition check isn't necessary -- it's the same one that $output will do internally anyway.
  72. // We only do it for efficiency here as the message formatting is relatively expensive.
  73. if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) {
  74. $output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]);
  75. }
  76. }
  77. /**
  78. * Returns true when any messages have been logged at error levels.
  79. *
  80. * @return bool
  81. */
  82. public function hasErrored()
  83. {
  84. return $this->errored;
  85. }
  86. /**
  87. * Interpolates context values into the message placeholders.
  88. *
  89. * @author PHP Framework Interoperability Group
  90. */
  91. private function interpolate(string $message, array $context): string
  92. {
  93. if (false === strpos($message, '{')) {
  94. return $message;
  95. }
  96. $replacements = [];
  97. foreach ($context as $key => $val) {
  98. if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
  99. $replacements["{{$key}}"] = $val;
  100. } elseif ($val instanceof \DateTimeInterface) {
  101. $replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
  102. } elseif (\is_object($val)) {
  103. $replacements["{{$key}}"] = '[object '.\get_class($val).']';
  104. } else {
  105. $replacements["{{$key}}"] = '['.\gettype($val).']';
  106. }
  107. }
  108. return strtr($message, $replacements);
  109. }
  110. }