annotate vendor/symfony/console/Logger/ConsoleLogger.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
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@0 17 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 18 use Symfony\Component\Console\Output\ConsoleOutputInterface;
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 /**
Chris@0 33 * @var OutputInterface
Chris@0 34 */
Chris@0 35 private $output;
Chris@0 36 /**
Chris@0 37 * @var array
Chris@0 38 */
Chris@0 39 private $verbosityLevelMap = array(
Chris@0 40 LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
Chris@0 41 LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
Chris@0 42 LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
Chris@0 43 LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
Chris@0 44 LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
Chris@0 45 LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE,
Chris@0 46 LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE,
Chris@0 47 LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG,
Chris@0 48 );
Chris@0 49 /**
Chris@0 50 * @var array
Chris@0 51 */
Chris@0 52 private $formatLevelMap = array(
Chris@0 53 LogLevel::EMERGENCY => self::ERROR,
Chris@0 54 LogLevel::ALERT => self::ERROR,
Chris@0 55 LogLevel::CRITICAL => self::ERROR,
Chris@0 56 LogLevel::ERROR => self::ERROR,
Chris@0 57 LogLevel::WARNING => self::INFO,
Chris@0 58 LogLevel::NOTICE => self::INFO,
Chris@0 59 LogLevel::INFO => self::INFO,
Chris@0 60 LogLevel::DEBUG => self::INFO,
Chris@0 61 );
Chris@0 62 private $errored = false;
Chris@0 63
Chris@0 64 /**
Chris@0 65 * @param OutputInterface $output
Chris@0 66 * @param array $verbosityLevelMap
Chris@0 67 * @param array $formatLevelMap
Chris@0 68 */
Chris@0 69 public function __construct(OutputInterface $output, array $verbosityLevelMap = array(), array $formatLevelMap = array())
Chris@0 70 {
Chris@0 71 $this->output = $output;
Chris@0 72 $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
Chris@0 73 $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * {@inheritdoc}
Chris@0 78 */
Chris@0 79 public function log($level, $message, array $context = array())
Chris@0 80 {
Chris@0 81 if (!isset($this->verbosityLevelMap[$level])) {
Chris@0 82 throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
Chris@0 83 }
Chris@0 84
Chris@0 85 $output = $this->output;
Chris@0 86
Chris@0 87 // Write to the error output if necessary and available
Chris@0 88 if ($this->formatLevelMap[$level] === self::ERROR) {
Chris@0 89 if ($this->output instanceof ConsoleOutputInterface) {
Chris@0 90 $output = $output->getErrorOutput();
Chris@0 91 }
Chris@0 92 $this->errored = true;
Chris@0 93 }
Chris@0 94
Chris@0 95 // the if condition check isn't necessary -- it's the same one that $output will do internally anyway.
Chris@0 96 // We only do it for efficiency here as the message formatting is relatively expensive.
Chris@0 97 if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) {
Chris@0 98 $output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]);
Chris@0 99 }
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * Returns true when any messages have been logged at error levels.
Chris@0 104 */
Chris@0 105 public function hasErrored()
Chris@0 106 {
Chris@0 107 return $this->errored;
Chris@0 108 }
Chris@0 109
Chris@0 110 /**
Chris@0 111 * Interpolates context values into the message placeholders.
Chris@0 112 *
Chris@0 113 * @author PHP Framework Interoperability Group
Chris@0 114 *
Chris@0 115 * @param string $message
Chris@0 116 * @param array $context
Chris@0 117 *
Chris@0 118 * @return string
Chris@0 119 */
Chris@0 120 private function interpolate($message, array $context)
Chris@0 121 {
Chris@0 122 // build a replacement array with braces around the context keys
Chris@0 123 $replace = array();
Chris@0 124 foreach ($context as $key => $val) {
Chris@0 125 if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
Chris@0 126 $replace[sprintf('{%s}', $key)] = $val;
Chris@0 127 }
Chris@0 128 }
Chris@0 129
Chris@0 130 // interpolate replacement values into the message and return
Chris@0 131 return strtr($message, $replace);
Chris@0 132 }
Chris@0 133 }