Commit 2c0f5e10 authored by alexpott's avatar alexpott
Browse files

Issue #298768 by mkalkbrenner, stefan.r: Ensure that entries are written to watchdog table

parent 5d3a761e
......@@ -9,7 +9,8 @@
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Database\Connection;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
......@@ -22,12 +23,17 @@ class DbLog implements LoggerInterface {
use RfcLoggerTrait;
use DependencySerializationTrait;
/**
* The dedicated database connection target to use for log entries.
*/
const DEDICATED_DBLOG_CONNECTION_TARGET = 'dedicated_dblog';
/**
* The database connection object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
protected $connection;
/**
* The message's placeholders parser.
......@@ -39,13 +45,13 @@ class DbLog implements LoggerInterface {
/**
* Constructs a DbLog object.
*
* @param \Drupal\Core\Database\Connection $database
* @param \Drupal\Core\Database\Connection $connection
* The database connection object.
* @param \Drupal\Core\Logger\LogMessageParserInterface $parser
* The parser to use when extracting message variables.
*/
public function __construct(Connection $database, LogMessageParserInterface $parser) {
$this->database = $database;
public function __construct(Connection $connection, LogMessageParserInterface $parser) {
$this->connection = $connection;
$this->parser = $parser;
}
......@@ -60,21 +66,52 @@ public function log($level, $message, array $context = array()) {
// translated too in runtime.
$message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);
$this->database
->insert('watchdog')
->fields(array(
'uid' => $context['uid'],
'type' => Unicode::substr($context['channel'], 0, 64),
'message' => $message,
'variables' => serialize($message_placeholders),
'severity' => $level,
'link' => $context['link'],
'location' => $context['request_uri'],
'referer' => $context['referer'],
'hostname' => Unicode::substr($context['ip'], 0, 128),
'timestamp' => $context['timestamp'],
))
->execute();
try {
$this->connection
->insert('watchdog')
->fields(array(
'uid' => $context['uid'],
'type' => Unicode::substr($context['channel'], 0, 64),
'message' => $message,
'variables' => serialize($message_placeholders),
'severity' => $level,
'link' => $context['link'],
'location' => $context['request_uri'],
'referer' => $context['referer'],
'hostname' => Unicode::substr($context['ip'], 0, 128),
'timestamp' => $context['timestamp'],
))
->execute();
}
catch (\Exception $e) {
// When running Drupal on MySQL or MariaDB you can run into several errors
// that corrupt the database connection. Some examples for these kind of
// errors on the database layer are "1100 - Table 'xyz' was not locked
// with LOCK TABLES" and "1153 - Got a packet bigger than
// 'max_allowed_packet' bytes". If such an error happens, the MySQL server
// invalidates the connection and answers all further requests in this
// connection with "2006 - MySQL server had gone away". In that case the
// insert statement above results in a database exception. To ensure that
// the causal error is written to the log we try once to open a dedicated
// connection and write again.
if (
// Only handle database related exceptions.
($e instanceof DatabaseException || $e instanceof \PDOException) &&
// Avoid an endless loop of re-write attempts.
$this->connection->getTarget() != self::DEDICATED_DBLOG_CONNECTION_TARGET
) {
// Open a dedicated connection for logging.
$key = $this->connection->getKey();
$info = Database::getConnectionInfo($key);
Database::addConnectionInfo($key, self::DEDICATED_DBLOG_CONNECTION_TARGET, $info['default']);
$this->connection = Database::getConnection(self::DEDICATED_DBLOG_CONNECTION_TARGET, $key);
// Now try once to log the error again.
$this->log($level, $message, $context);
}
else {
throw $e;
}
}
}
}
<?php
/**
* @file
* Definition of \Drupal\dblog\Tests\ConnectionFailureTest.
*/
namespace Drupal\dblog\Tests;
use Drupal\Core\Database\Database;
use Drupal\simpletest\WebTestBase;
/**
* Tests logging of connection failures.
*
* @group dblog
*/
class ConnectionFailureTest extends WebTestBase {
public static $modules = array('dblog');
/**
* Tests logging of connection failures.
*/
function testConnectionFailureLogging() {
$logger = \Drupal::service('logger.factory');
// MySQL errors like "1153 - Got a packet bigger than 'max_allowed_packet'
// bytes" or "1100 - Table 'xyz' was not locked with LOCK TABLES" lead to a
// database connection unusable for further requests. All further request
// will result in an "2006 - MySQL server had gone away" error. As a
// consequence it's impossible to use this connection to log the causing
// initial error itself. Using Database::closeConnection() we simulate such
// a corrupted connection. In this case dblog has to establish a different
// connection by itself to be able to write the log entry.
Database::closeConnection();
// Create a log entry.
$logger->get('php')->error('testConnectionFailureLogging');
// Re-establish the default database connection.
Database::getConnection();
$wid = db_query("SELECT MAX(wid) FROM {watchdog} WHERE message = 'testConnectionFailureLogging'")->fetchField();
$this->assertTrue($wid, 'Watchdog entry has been stored in database.');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment