annotate vendor/symfony/console/Logger/ConsoleLogger.php @ 19:fa3358dc1485 tip

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