You are here

class ConsoleLogger in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/console/Logger/ConsoleLogger.php \Symfony\Component\Console\Logger\ConsoleLogger

PSR-3 compliant console logger.

@author Kévin Dunglas <dunglas@gmail.com>

@link http://www.php-fig.org/psr/psr-3/

Hierarchy

Expanded class hierarchy of ConsoleLogger

1 file declares its use of ConsoleLogger
ConsoleLoggerTest.php in vendor/symfony/console/Tests/Logger/ConsoleLoggerTest.php

File

vendor/symfony/console/Logger/ConsoleLogger.php, line 27

Namespace

Symfony\Component\Console\Logger
View source
class ConsoleLogger extends AbstractLogger {
  const INFO = 'info';
  const ERROR = 'error';

  /**
   * @var OutputInterface
   */
  private $output;

  /**
   * @var array
   */
  private $verbosityLevelMap = array(
    LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
    LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
    LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
    LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
    LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
    LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE,
    LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE,
    LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG,
  );

  /**
   * @var array
   */
  private $formatLevelMap = array(
    LogLevel::EMERGENCY => self::ERROR,
    LogLevel::ALERT => self::ERROR,
    LogLevel::CRITICAL => self::ERROR,
    LogLevel::ERROR => self::ERROR,
    LogLevel::WARNING => self::INFO,
    LogLevel::NOTICE => self::INFO,
    LogLevel::INFO => self::INFO,
    LogLevel::DEBUG => self::INFO,
  );

  /**
   * @param OutputInterface $output
   * @param array           $verbosityLevelMap
   * @param array           $formatLevelMap
   */
  public function __construct(OutputInterface $output, array $verbosityLevelMap = array(), array $formatLevelMap = array()) {
    $this->output = $output;
    $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
    $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
  }

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = array()) {
    if (!isset($this->verbosityLevelMap[$level])) {
      throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
    }

    // Write to the error output if necessary and available
    if ($this->formatLevelMap[$level] === self::ERROR && $this->output instanceof ConsoleOutputInterface) {
      $output = $this->output
        ->getErrorOutput();
    }
    else {
      $output = $this->output;
    }
    if ($output
      ->getVerbosity() >= $this->verbosityLevelMap[$level]) {
      $output
        ->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this
        ->interpolate($message, $context)));
    }
  }

  /**
   * Interpolates context values into the message placeholders.
   *
   * @author PHP Framework Interoperability Group
   *
   * @param string $message
   * @param array  $context
   *
   * @return string
   */
  private function interpolate($message, array $context) {

    // build a replacement array with braces around the context keys
    $replace = array();
    foreach ($context as $key => $val) {
      if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
        $replace[sprintf('{%s}', $key)] = $val;
      }
    }

    // interpolate replacement values into the message and return
    return strtr($message, $replace);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractLogger::alert public function Action must be taken immediately. Overrides LoggerInterface::alert
AbstractLogger::critical public function Critical conditions. Overrides LoggerInterface::critical
AbstractLogger::debug public function Detailed debug information. Overrides LoggerInterface::debug
AbstractLogger::emergency public function System is unusable. Overrides LoggerInterface::emergency
AbstractLogger::error public function Runtime errors that do not require immediate action but should typically be logged and monitored. Overrides LoggerInterface::error
AbstractLogger::info public function Interesting events. Overrides LoggerInterface::info
AbstractLogger::notice public function Normal but significant events. Overrides LoggerInterface::notice
AbstractLogger::warning public function Exceptional occurrences that are not errors. Overrides LoggerInterface::warning
ConsoleLogger::$formatLevelMap private property
ConsoleLogger::$output private property
ConsoleLogger::$verbosityLevelMap private property
ConsoleLogger::ERROR constant
ConsoleLogger::INFO constant
ConsoleLogger::interpolate private function Interpolates context values into the message placeholders.
ConsoleLogger::log public function Logs with an arbitrary level. Overrides LoggerInterface::log
ConsoleLogger::__construct public function