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