5933e222810c35c183d20cbf45ba1b93a9d3776a
[yaffs-website] / web / core / modules / dblog / src / Logger / DbLog.php
1 <?php
2
3 namespace Drupal\dblog\Logger;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Database\DatabaseException;
8 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
9 use Drupal\Core\Logger\LogMessageParserInterface;
10 use Drupal\Core\Logger\RfcLoggerTrait;
11 use Psr\Log\LoggerInterface;
12
13 /**
14  * Logs events in the watchdog database table.
15  */
16 class DbLog implements LoggerInterface {
17   use RfcLoggerTrait;
18   use DependencySerializationTrait;
19
20   /**
21    * The dedicated database connection target to use for log entries.
22    */
23   const DEDICATED_DBLOG_CONNECTION_TARGET = 'dedicated_dblog';
24
25   /**
26    * The database connection object.
27    *
28    * @var \Drupal\Core\Database\Connection
29    */
30   protected $connection;
31
32   /**
33    * The message's placeholders parser.
34    *
35    * @var \Drupal\Core\Logger\LogMessageParserInterface
36    */
37   protected $parser;
38
39   /**
40    * Constructs a DbLog object.
41    *
42    * @param \Drupal\Core\Database\Connection $connection
43    *   The database connection object.
44    * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
45    *   The parser to use when extracting message variables.
46    */
47   public function __construct(Connection $connection, LogMessageParserInterface $parser) {
48     $this->connection = $connection;
49     $this->parser = $parser;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function log($level, $message, array $context = []) {
56     // Remove any backtraces since they may contain an unserializable variable.
57     unset($context['backtrace']);
58
59     // Convert PSR3-style messages to \Drupal\Component\Render\FormattableMarkup
60     // style, so they can be translated too in runtime.
61     $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);
62
63     try {
64       $this->connection
65         ->insert('watchdog')
66         ->fields([
67           'uid' => $context['uid'],
68           'type' => mb_substr($context['channel'], 0, 64),
69           'message' => $message,
70           'variables' => serialize($message_placeholders),
71           'severity' => $level,
72           'link' => $context['link'],
73           'location' => $context['request_uri'],
74           'referer' => $context['referer'],
75           'hostname' => mb_substr($context['ip'], 0, 128),
76           'timestamp' => $context['timestamp'],
77         ])
78         ->execute();
79     }
80     catch (\Exception $e) {
81       // When running Drupal on MySQL or MariaDB you can run into several errors
82       // that corrupt the database connection. Some examples for these kind of
83       // errors on the database layer are "1100 - Table 'xyz' was not locked
84       // with LOCK TABLES" and "1153 - Got a packet bigger than
85       // 'max_allowed_packet' bytes". If such an error happens, the MySQL server
86       // invalidates the connection and answers all further requests in this
87       // connection with "2006 - MySQL server had gone away". In that case the
88       // insert statement above results in a database exception. To ensure that
89       // the causal error is written to the log we try once to open a dedicated
90       // connection and write again.
91       if (
92         // Only handle database related exceptions.
93         ($e instanceof DatabaseException || $e instanceof \PDOException) &&
94         // Avoid an endless loop of re-write attempts.
95         $this->connection->getTarget() != self::DEDICATED_DBLOG_CONNECTION_TARGET
96       ) {
97         // Open a dedicated connection for logging.
98         $key = $this->connection->getKey();
99         $info = Database::getConnectionInfo($key);
100         Database::addConnectionInfo($key, self::DEDICATED_DBLOG_CONNECTION_TARGET, $info['default']);
101         $this->connection = Database::getConnection(self::DEDICATED_DBLOG_CONNECTION_TARGET, $key);
102         // Now try once to log the error again.
103         $this->log($level, $message, $context);
104       }
105       else {
106         throw $e;
107       }
108     }
109   }
110
111 }