Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / drush / drush / src / Log / DrushLog.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drush\Log\DrushLog.
6  *
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.
11  *
12  * See Drush\Log\Logger for our actuall LoggerInterface
13  * implementation, that does the work of logging messages
14  * that originate from Drush.
15  */
16
17 namespace Drush\Log;
18
19 use Drupal\Core\Logger\LogMessageParserInterface;
20 use Drupal\Core\Logger\RfcLoggerTrait;
21 use Drupal\Core\Logger\RfcLogLevel;
22 use Psr\Log\LoggerInterface;
23
24 /**
25  * Redirects Drupal logging messages to Drush log.
26  *
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.
31  */
32 class DrushLog implements LoggerInterface
33 {
34
35     use RfcLoggerTrait;
36
37     /**
38      * The message's placeholders parser.
39      *
40      * @var \Drupal\Core\Logger\LogMessageParserInterface
41      */
42     protected $parser;
43
44     /**
45      * The logger that messages will be passed through to.
46      */
47     protected $logger;
48
49     /**
50      * Constructs a DrushLog object.
51      *
52      * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
53      *   The parser to use when extracting message variables.
54      */
55     public function __construct(LogMessageParserInterface $parser, LoggerInterface $logger)
56     {
57         $this->parser = $parser;
58         $this->logger = $logger;
59     }
60
61     /**
62      * {@inheritdoc}
63      */
64     public function log($level, $message, array $context = [])
65     {
66         // Translate the RFC logging levels into their Drush counterparts, more or
67         // less.
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,
70         // though.
71         switch ($level) {
72             case RfcLogLevel::ALERT:
73             case RfcLogLevel::CRITICAL:
74             case RfcLogLevel::EMERGENCY:
75             case RfcLogLevel::ERROR:
76                 $error_type = LogLevel::ERROR;
77                 break;
78
79             case RfcLogLevel::WARNING:
80                 $error_type = LogLevel::WARNING;
81                 break;
82
83             case RfcLogLevel::DEBUG:
84                 $error_type = LogLevel::DEBUG;
85                 break;
86
87             case RfcLogLevel::INFO:
88                 $error_type = LogLevel::INFO;
89                 break;
90
91             case RfcLogLevel::NOTICE:
92                 $error_type = LogLevel::NOTICE;
93                 break;
94
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'.
99             default:
100                 $error_type = $level;
101                 break;
102         }
103
104         // Populate the message placeholders and then replace them in the message.
105         $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);
106
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']);
110         });
111
112         $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
113
114         $this->logger->log($error_type, $message, $context);
115     }
116 }