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\ConsoleOutputInterface;
18 use Symfony\Component\Console\Output\OutputInterface;
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 private $verbosityLevelMap = array(
34 LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
35 LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
36 LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
37 LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
38 LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
39 LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE,
40 LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE,
41 LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG,
43 private $formatLevelMap = array(
44 LogLevel::EMERGENCY => self::ERROR,
45 LogLevel::ALERT => self::ERROR,
46 LogLevel::CRITICAL => self::ERROR,
47 LogLevel::ERROR => self::ERROR,
48 LogLevel::WARNING => self::INFO,
49 LogLevel::NOTICE => self::INFO,
50 LogLevel::INFO => self::INFO,
51 LogLevel::DEBUG => self::INFO,
53 private $errored = false;
55 public function __construct(OutputInterface $output, array $verbosityLevelMap = array(), array $formatLevelMap = array())
57 $this->output = $output;
58 $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
59 $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
65 public function log($level, $message, array $context = array())
67 if (!isset($this->verbosityLevelMap[$level])) {
68 throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
71 $output = $this->output;
73 // Write to the error output if necessary and available
74 if (self::ERROR === $this->formatLevelMap[$level]) {
75 if ($this->output instanceof ConsoleOutputInterface) {
76 $output = $output->getErrorOutput();
78 $this->errored = true;
81 // the if condition check isn't necessary -- it's the same one that $output will do internally anyway.
82 // We only do it for efficiency here as the message formatting is relatively expensive.
83 if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) {
84 $output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]);
89 * Returns true when any messages have been logged at error levels.
93 public function hasErrored()
95 return $this->errored;
99 * Interpolates context values into the message placeholders.
101 * @author PHP Framework Interoperability Group
103 * @param string $message
104 * @param array $context
108 private function interpolate($message, array $context)
110 if (false === strpos($message, '{')) {
114 $replacements = array();
115 foreach ($context as $key => $val) {
116 if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
117 $replacements["{{$key}}"] = $val;
118 } elseif ($val instanceof \DateTimeInterface) {
119 $replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
120 } elseif (\is_object($val)) {
121 $replacements["{{$key}}"] = '[object '.\get_class($val).']';
123 $replacements["{{$key}}"] = '['.\gettype($val).']';
127 return strtr($message, $replacements);