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 ascending 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. * The logger that messages will be passed through to.
  38. */
  39. protected $logger;
  40. /**
  41. * Constructs a DrushLog object.
  42. *
  43. * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
  44. * The parser to use when extracting message variables.
  45. */
  46. public function __construct(LogMessageParserInterface $parser, LoggerInterface $logger) {
  47. $this->parser = $parser;
  48. $this->logger = $logger;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function log($level, $message, array $context = array()) {
  54. // Translate the RFC logging levels into their Drush counterparts, more or
  55. // less.
  56. // @todo ALERT, CRITICAL and EMERGENCY are considered show-stopping errors,
  57. // and they should cause Drush to exit or panic. Not sure how to handle this,
  58. // though.
  59. switch ($level) {
  60. case RfcLogLevel::ALERT:
  61. case RfcLogLevel::CRITICAL:
  62. case RfcLogLevel::EMERGENCY:
  63. case RfcLogLevel::ERROR:
  64. $error_type = LogLevel::ERROR;
  65. break;
  66. case RfcLogLevel::WARNING:
  67. $error_type = LogLevel::WARNING;
  68. break;
  69. // TODO: RfcLogLevel::DEBUG should be 'debug' rather than 'notice'?
  70. case RfcLogLevel::DEBUG:
  71. case RfcLogLevel::INFO:
  72. case RfcLogLevel::NOTICE:
  73. $error_type = LogLevel::NOTICE;
  74. break;
  75. // TODO: Unknown log levels that are not defined
  76. // in Psr\Log\LogLevel or Drush\Log\LogLevel SHOULD NOT be used. See
  77. // https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
  78. // We should convert these to 'notice'.
  79. default:
  80. $error_type = $level;
  81. break;
  82. }
  83. // Populate the message placeholders and then replace them in the message.
  84. $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);
  85. $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
  86. $this->logger->log($error_type, $message, $context);
  87. }
  88. }