4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Logger;
14 use Psr\Log\AbstractLogger;
15 use Psr\Log\InvalidArgumentException;
17 use Symfony\Component\Console\Output\OutputInterface;
18 use Symfony\Component\Console\Output\ConsoleOutputInterface;
21 * PSR-3 compliant console logger.
23 * @author Kévin Dunglas <dunglas@gmail.com>
25 * @see http://www.php-fig.org/psr/psr-3/
27 class ConsoleLogger extends AbstractLogger
30 const ERROR = 'error';
33 * @var OutputInterface
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,
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,
64 * @param OutputInterface $output
65 * @param array $verbosityLevelMap
66 * @param array $formatLevelMap
68 public function __construct(OutputInterface $output, array $verbosityLevelMap = array(), array $formatLevelMap = array())
70 $this->output = $output;
71 $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
72 $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
78 public function log($level, $message, array $context = array())
80 if (!isset($this->verbosityLevelMap[$level])) {
81 throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
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();
88 $output = $this->output;
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)));
97 * Interpolates context values into the message placeholders.
99 * @author PHP Framework Interoperability Group
101 * @param string $message
102 * @param array $context
106 private function interpolate($message, array $context)
108 // build a replacement array with braces around the context keys
110 foreach ($context as $key => $val) {
111 if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
112 $replace[sprintf('{%s}', $key)] = $val;
116 // interpolate replacement values into the message and return
117 return strtr($message, $replace);