DrushLog.php

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

Contains \Drush\Log\DrushLog.

This class is only used to convert logging calls made inside of Drupal into a logging format that is usable by Drush. This code is ONLY usable within the context of a bootstrapped Drupal 8 site.

See Drush\Log\Logger for our actuall LoggerInterface implementation, that does the work of logging messages that originate from Drush.

Namespace

Drush\Log

Classes

Namesort descending Description
DrushLog Redirects Drupal logging messages to Drush log.

File

lib/Drush/Log/DrushLog.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drush\Log\DrushLog.
  5. *
  6. * This class is only used to convert logging calls made
  7. * inside of Drupal into a logging format that is usable
  8. * by Drush. This code is ONLY usable within the context
  9. * of a bootstrapped Drupal 8 site.
  10. *
  11. * See Drush\Log\Logger for our actuall LoggerInterface
  12. * implementation, that does the work of logging messages
  13. * that originate from Drush.
  14. */
  15. namespace Drush\Log;
  16. use Drupal\Core\Logger\LogMessageParserInterface;
  17. use Drupal\Core\Logger\RfcLoggerTrait;
  18. use Drupal\Core\Logger\RfcLogLevel;
  19. use Psr\Log\LoggerInterface;
  20. /**
  21. * Redirects Drupal logging messages to Drush log.
  22. *
  23. * Note that Drupal extends the LoggerInterface, and
  24. * needlessly replaces Psr\Log\LogLevels with Drupal\Core\Logger\RfcLogLevel.
  25. * Doing this arguably violates the Psr\Log contract,
  26. * but we can't help that here -- we just need to convert back.
  27. */
  28. class DrushLog implements LoggerInterface {
  29. use RfcLoggerTrait;
  30. /**
  31. * The message's placeholders parser.
  32. *
  33. * @var \Drupal\Core\Logger\LogMessageParserInterface
  34. */
  35. protected $parser;
  36. /**
  37. * Constructs a DrushLog object.
  38. *
  39. * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
  40. * The parser to use when extracting message variables.
  41. */
  42. public function __construct(LogMessageParserInterface $parser) {
  43. $this->parser = $parser;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function log($level, $message, array $context = array()) {
  49. // Translate the RFC logging levels into their Drush counterparts, more or
  50. // less.
  51. // @todo ALERT, CRITICAL and EMERGENCY are considered show-stopping errors,
  52. // and they should cause Drush to exit or panic. Not sure how to handle this,
  53. // though.
  54. switch ($level) {
  55. case RfcLogLevel::ALERT:
  56. case RfcLogLevel::CRITICAL:
  57. case RfcLogLevel::EMERGENCY:
  58. case RfcLogLevel::ERROR:
  59. $error_type = LogLevel::ERROR;
  60. break;
  61. case RfcLogLevel::WARNING:
  62. $error_type = LogLevel::WARNING;
  63. break;
  64. // TODO: RfcLogLevel::DEBUG should be 'debug' rather than 'notice'?
  65. case RfcLogLevel::DEBUG:
  66. case RfcLogLevel::INFO:
  67. case RfcLogLevel::NOTICE:
  68. $error_type = LogLevel::NOTICE;
  69. break;
  70. // TODO: Unknown log levels that are not defined
  71. // in Psr\Log\LogLevel SHOULD NOT be used. See
  72. // https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
  73. // We should convert these to 'notice'.
  74. default:
  75. $error_type = $level;
  76. break;
  77. }
  78. // Populate the message placeholders and then replace them in the message.
  79. $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);
  80. $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
  81. drush_log($message, $error_type);
  82. }
  83. }