comparison vendor/symfony/console/Logger/ConsoleLogger.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 private $errored = false;
63
64 /**
65 * @param OutputInterface $output
66 * @param array $verbosityLevelMap
67 * @param array $formatLevelMap
68 */
69 public function __construct(OutputInterface $output, array $verbosityLevelMap = array(), array $formatLevelMap = array())
70 {
71 $this->output = $output;
72 $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
73 $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
74 }
75
76 /**
77 * {@inheritdoc}
78 */
79 public function log($level, $message, array $context = array())
80 {
81 if (!isset($this->verbosityLevelMap[$level])) {
82 throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
83 }
84
85 $output = $this->output;
86
87 // Write to the error output if necessary and available
88 if ($this->formatLevelMap[$level] === self::ERROR) {
89 if ($this->output instanceof ConsoleOutputInterface) {
90 $output = $output->getErrorOutput();
91 }
92 $this->errored = true;
93 }
94
95 // the if condition check isn't necessary -- it's the same one that $output will do internally anyway.
96 // We only do it for efficiency here as the message formatting is relatively expensive.
97 if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) {
98 $output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]);
99 }
100 }
101
102 /**
103 * Returns true when any messages have been logged at error levels.
104 */
105 public function hasErrored()
106 {
107 return $this->errored;
108 }
109
110 /**
111 * Interpolates context values into the message placeholders.
112 *
113 * @author PHP Framework Interoperability Group
114 *
115 * @param string $message
116 * @param array $context
117 *
118 * @return string
119 */
120 private function interpolate($message, array $context)
121 {
122 // build a replacement array with braces around the context keys
123 $replace = array();
124 foreach ($context as $key => $val) {
125 if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
126 $replace[sprintf('{%s}', $key)] = $val;
127 }
128 }
129
130 // interpolate replacement values into the message and return
131 return strtr($message, $replace);
132 }
133 }