ConsoleLogger.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\OutputInterface;
  15. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  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. /**
  28. * @var OutputInterface
  29. */
  30. private $output;
  31. /**
  32. * @var array
  33. */
  34. private $verbosityLevelMap = array(
  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 array
  46. */
  47. private $formatLevelMap = array(
  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. private $errored = false;
  58. /**
  59. * @param OutputInterface $output
  60. * @param array $verbosityLevelMap
  61. * @param array $formatLevelMap
  62. */
  63. public function __construct(OutputInterface $output, array $verbosityLevelMap = array(), array $formatLevelMap = array())
  64. {
  65. $this->output = $output;
  66. $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
  67. $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function log($level, $message, array $context = array())
  73. {
  74. if (!isset($this->verbosityLevelMap[$level])) {
  75. throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
  76. }
  77. $output = $this->output;
  78. // Write to the error output if necessary and available
  79. if ($this->formatLevelMap[$level] === self::ERROR) {
  80. if ($this->output instanceof ConsoleOutputInterface) {
  81. $output = $output->getErrorOutput();
  82. }
  83. $this->errored = true;
  84. }
  85. // the if condition check isn't necessary -- it's the same one that $output will do internally anyway.
  86. // We only do it for efficiency here as the message formatting is relatively expensive.
  87. if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) {
  88. $output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]);
  89. }
  90. }
  91. /**
  92. * Returns true when any messages have been logged at error levels.
  93. */
  94. public function hasErrored()
  95. {
  96. return $this->errored;
  97. }
  98. /**
  99. * Interpolates context values into the message placeholders.
  100. *
  101. * @author PHP Framework Interoperability Group
  102. *
  103. * @param string $message
  104. * @param array $context
  105. *
  106. * @return string
  107. */
  108. private function interpolate($message, array $context)
  109. {
  110. // build a replacement array with braces around the context keys
  111. $replace = array();
  112. foreach ($context as $key => $val) {
  113. if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
  114. $replace[sprintf('{%s}', $key)] = $val;
  115. }
  116. }
  117. // interpolate replacement values into the message and return
  118. return strtr($message, $replace);
  119. }
  120. }