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