TYPO3  7.6
ConsoleLogger.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Logger;
13 
19 
28 {
29  const INFO = 'info';
30  const ERROR = 'error';
31 
35  private $output;
39  private $verbosityLevelMap = array(
48  );
52  private $formatLevelMap = array(
53  LogLevel::EMERGENCY => self::ERROR,
54  LogLevel::ALERT => self::ERROR,
55  LogLevel::CRITICAL => self::ERROR,
56  LogLevel::ERROR => self::ERROR,
57  LogLevel::WARNING => self::INFO,
58  LogLevel::NOTICE => self::INFO,
59  LogLevel::INFO => self::INFO,
60  LogLevel::DEBUG => self::INFO,
61  );
62 
68  public function __construct(OutputInterface $output, array $verbosityLevelMap = array(), array $formatLevelMap = array())
69  {
70  $this->output = $output;
71  $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
72  $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
73  }
74 
78  public function log($level, $message, array $context = array())
79  {
80  if (!isset($this->verbosityLevelMap[$level])) {
81  throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
82  }
83 
84  // Write to the error output if necessary and available
85  if ($this->formatLevelMap[$level] === self::ERROR && $this->output instanceof ConsoleOutputInterface) {
86  $output = $this->output->getErrorOutput();
87  } else {
88  $output = $this->output;
89  }
90 
91  if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) {
92  $output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)));
93  }
94  }
95 
106  private function interpolate($message, array $context)
107  {
108  // build a replacement array with braces around the context keys
109  $replace = array();
110  foreach ($context as $key => $val) {
111  if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
112  $replace[sprintf('{%s}', $key)] = $val;
113  }
114  }
115 
116  // interpolate replacement values into the message and return
117  return strtr($message, $replace);
118  }
119 }