987e96a6587e5aecaa2fcddccd2533fae843b9cd
[yaffs-website] / vendor / symfony / console / Logger / ConsoleLogger.php
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
14 use Psr\Log\AbstractLogger;
15 use Psr\Log\InvalidArgumentException;
16 use Psr\Log\LogLevel;
17 use Symfony\Component\Console\Output\OutputInterface;
18 use Symfony\Component\Console\Output\ConsoleOutputInterface;
19
20 /**
21  * PSR-3 compliant console logger.
22  *
23  * @author Kévin Dunglas <dunglas@gmail.com>
24  *
25  * @see http://www.php-fig.org/psr/psr-3/
26  */
27 class ConsoleLogger extends AbstractLogger
28 {
29     const INFO = 'info';
30     const ERROR = 'error';
31
32     /**
33      * @var OutputInterface
34      */
35     private $output;
36     /**
37      * @var array
38      */
39     private $verbosityLevelMap = array(
40         LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
41         LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
42         LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
43         LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
44         LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
45         LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE,
46         LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE,
47         LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG,
48     );
49     /**
50      * @var array
51      */
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
63     /**
64      * @param OutputInterface $output
65      * @param array           $verbosityLevelMap
66      * @param array           $formatLevelMap
67      */
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
75     /**
76      * {@inheritdoc}
77      */
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
96     /**
97      * Interpolates context values into the message placeholders.
98      *
99      * @author PHP Framework Interoperability Group
100      *
101      * @param string $message
102      * @param array  $context
103      *
104      * @return string
105      */
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 }