class DrushLog

  1. 8.0.x lib/Drush/Log/DrushLog.php DrushLog
  2. 7.x lib/Drush/Log/DrushLog.php DrushLog
  3. master lib/Drush/Log/DrushLog.php DrushLog

Redirects Drupal logging messages to Drush log.

Note that Drupal extends the LoggerInterface, and needlessly replaces Psr\Log\LogLevels with Drupal\Core\Logger\RfcLogLevel. Doing this arguably violates the Psr\Log contract, but we can't help that here -- we just need to convert back.

Namespace

Drush\Log

Hierarchy

  • class DrushLog implements \Psr\Log\LoggerInterface

Expanded class hierarchy of DrushLog

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
DrushLog::$parser protected property The message's placeholders parser.
DrushLog::log public function
DrushLog::__construct public function Constructs a DrushLog object.

File

lib/Drush/Log/DrushLog.php, line 32
Contains \Drush\Log\DrushLog.

View source
class DrushLog implements LoggerInterface {

  use RfcLoggerTrait;

  /**
   * The message's placeholders parser.
   *
   * @var \Drupal\Core\Logger\LogMessageParserInterface
   */
  protected $parser;

  /**
   * Constructs a DrushLog object.
   *
   * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
   *   The parser to use when extracting message variables.
   */
  public function __construct(LogMessageParserInterface $parser) {
    $this->parser = $parser;
  }

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = array()) {
    // Translate the RFC logging levels into their Drush counterparts, more or
    // less.
    // @todo ALERT, CRITICAL and EMERGENCY are considered show-stopping errors,
    // and they should cause Drush to exit or panic. Not sure how to handle this,
    // though.
    switch ($level) {
      case RfcLogLevel::ALERT:
      case RfcLogLevel::CRITICAL:
      case RfcLogLevel::EMERGENCY:
      case RfcLogLevel::ERROR:
        $error_type = LogLevel::ERROR;
        break;

      case RfcLogLevel::WARNING:
        $error_type = LogLevel::WARNING;
        break;

        // TODO: RfcLogLevel::DEBUG should be 'debug' rather than 'notice'?
      case RfcLogLevel::DEBUG:
      case RfcLogLevel::INFO:
      case RfcLogLevel::NOTICE:
        $error_type = LogLevel::NOTICE;
        break;

        // TODO: Unknown log levels that are not defined
        // in Psr\Log\LogLevel SHOULD NOT be used.  See
        // https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
        // We should convert these to 'notice'.
      default:
        $error_type = $level;
        break;
    }

    // Populate the message placeholders and then replace them in the message.
    $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);
    $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
    drush_log($message, $error_type);
  }

}