3 namespace Drupal\dblog\Logger;
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;
14 * Logs events in the watchdog database table.
16 class DbLog implements LoggerInterface {
18 use DependencySerializationTrait;
21 * The dedicated database connection target to use for log entries.
23 const DEDICATED_DBLOG_CONNECTION_TARGET = 'dedicated_dblog';
26 * The database connection object.
28 * @var \Drupal\Core\Database\Connection
30 protected $connection;
33 * The message's placeholders parser.
35 * @var \Drupal\Core\Logger\LogMessageParserInterface
40 * Constructs a DbLog object.
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.
47 public function __construct(Connection $connection, LogMessageParserInterface $parser) {
48 $this->connection = $connection;
49 $this->parser = $parser;
55 public function log($level, $message, array $context = []) {
56 // Remove any backtraces since they may contain an unserializable variable.
57 unset($context['backtrace']);
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);
67 'uid' => $context['uid'],
68 'type' => mb_substr($context['channel'], 0, 64),
69 'message' => $message,
70 'variables' => serialize($message_placeholders),
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'],
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.
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
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);