5 * Contains \Drush\Log\DrushLog.
7 * This class is only used to convert logging calls made
8 * inside of Drupal into a logging format that is usable
9 * by Drush. This code is ONLY usable within the context
10 * of a bootstrapped Drupal 8 site.
12 * See Drush\Log\Logger for our actuall LoggerInterface
13 * implementation, that does the work of logging messages
14 * that originate from Drush.
19 use Drupal\Core\Logger\LogMessageParserInterface;
20 use Drupal\Core\Logger\RfcLoggerTrait;
21 use Drupal\Core\Logger\RfcLogLevel;
22 use Psr\Log\LoggerInterface;
25 * Redirects Drupal logging messages to Drush log.
27 * Note that Drupal extends the LoggerInterface, and
28 * needlessly replaces Psr\Log\LogLevels with Drupal\Core\Logger\RfcLogLevel.
29 * Doing this arguably violates the Psr\Log contract,
30 * but we can't help that here -- we just need to convert back.
32 class DrushLog implements LoggerInterface
38 * The message's placeholders parser.
40 * @var \Drupal\Core\Logger\LogMessageParserInterface
45 * The logger that messages will be passed through to.
50 * Constructs a DrushLog object.
52 * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
53 * The parser to use when extracting message variables.
55 public function __construct(LogMessageParserInterface $parser, LoggerInterface $logger)
57 $this->parser = $parser;
58 $this->logger = $logger;
64 public function log($level, $message, array $context = [])
66 // Translate the RFC logging levels into their Drush counterparts, more or
68 // @todo ALERT, CRITICAL and EMERGENCY are considered show-stopping errors,
69 // and they should cause Drush to exit or panic. Not sure how to handle this,
72 case RfcLogLevel::ALERT:
73 case RfcLogLevel::CRITICAL:
74 case RfcLogLevel::EMERGENCY:
75 case RfcLogLevel::ERROR:
76 $error_type = LogLevel::ERROR;
79 case RfcLogLevel::WARNING:
80 $error_type = LogLevel::WARNING;
83 case RfcLogLevel::DEBUG:
84 $error_type = LogLevel::DEBUG;
87 case RfcLogLevel::INFO:
88 $error_type = LogLevel::INFO;
91 case RfcLogLevel::NOTICE:
92 $error_type = LogLevel::NOTICE;
95 // TODO: Unknown log levels that are not defined
96 // in Psr\Log\LogLevel or Drush\Log\LogLevel SHOULD NOT be used. See
97 // https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
98 // We should convert these to 'notice'.
100 $error_type = $level;
104 // Populate the message placeholders and then replace them in the message.
105 $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);
107 // Filter out any placeholders that can not be cast to strings.
108 $message_placeholders = array_filter($message_placeholders, function ($element) {
109 return is_scalar($element) || is_callable([$element, '__toString']);
112 $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
114 $this->logger->log($error_type, $message, $context);