ConsoleLogger.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. private $output;
  28. private $verbosityLevelMap = array(
  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 = array(
  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. public function __construct(OutputInterface $output, array $verbosityLevelMap = array(), array $formatLevelMap = array())
  49. {
  50. $this->output = $output;
  51. $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
  52. $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function log($level, $message, array $context = array())
  58. {
  59. if (!isset($this->verbosityLevelMap[$level])) {
  60. throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
  61. }
  62. // Write to the error output if necessary and available
  63. if (self::ERROR === $this->formatLevelMap[$level] && $this->output instanceof ConsoleOutputInterface) {
  64. $output = $this->output->getErrorOutput();
  65. } else {
  66. $output = $this->output;
  67. }
  68. if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) {
  69. $output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)));
  70. }
  71. }
  72. /**
  73. * Interpolates context values into the message placeholders.
  74. *
  75. * @author PHP Framework Interoperability Group
  76. *
  77. * @param string $message
  78. * @param array $context
  79. *
  80. * @return string
  81. */
  82. private function interpolate($message, array $context)
  83. {
  84. // build a replacement array with braces around the context keys
  85. $replace = array();
  86. foreach ($context as $key => $val) {
  87. if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
  88. $replace[sprintf('{%s}', $key)] = $val;
  89. }
  90. }
  91. // interpolate replacement values into the message and return
  92. return strtr($message, $replace);
  93. }
  94. }