From 97aa5e1f6b56d4149cf58f33e7fa16eedcaeea9a Mon Sep 17 00:00:00 2001 From: "stefan.r" Date: Fri, 12 Aug 2016 13:21:36 +0200 Subject: [PATCH] Issue #1784548 by btopro, Fabianx, Berdir, rbayliss, xaa: DB Log is missing watchdog table after enabling - Need to catch and ignore exceptions thrown in dblog_watchdog() --- CHANGELOG.txt | 1 + modules/dblog/dblog.install | 9 +++++++++ modules/dblog/dblog.module | 35 +++++++++++++++++++++-------------- modules/dblog/dblog.test | 27 +++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 383c29e101f..b567b133952 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,6 +1,7 @@ Drupal 7.51, xxxx-xx-xx (development version) ----------------------- +- Exceptions thrown in dblog_watchdog() are now caught and ignored. - Numerous API documentation improvements. Drupal 7.50, 2016-07-07 diff --git a/modules/dblog/dblog.install b/modules/dblog/dblog.install index abfd9a2c979..c2e41192198 100644 --- a/modules/dblog/dblog.install +++ b/modules/dblog/dblog.install @@ -154,6 +154,15 @@ function dblog_update_7002() { db_add_index('watchdog', 'severity', array('severity')); } +/** + * Account for possible legacy systems where dblog was not installed. + */ +function dblog_update_7003() { + if (!db_table_exists('watchdog')) { + db_create_table('watchdog', drupal_get_schema_unprocessed('dblog', 'watchdog')); + } +} + /** * @} End of "addtogroup updates-7.x-extra". */ diff --git a/modules/dblog/dblog.module b/modules/dblog/dblog.module index eb79faffcd2..df305a2c388 100644 --- a/modules/dblog/dblog.module +++ b/modules/dblog/dblog.module @@ -147,20 +147,27 @@ function dblog_watchdog(array $log_entry) { if (!function_exists('drupal_substr')) { require_once DRUPAL_ROOT . '/includes/unicode.inc'; } - Database::getConnection('default', 'default')->insert('watchdog') - ->fields(array( - 'uid' => $log_entry['uid'], - 'type' => drupal_substr($log_entry['type'], 0, 64), - 'message' => $log_entry['message'], - 'variables' => serialize($log_entry['variables']), - 'severity' => $log_entry['severity'], - 'link' => drupal_substr($log_entry['link'], 0, 255), - 'location' => $log_entry['request_uri'], - 'referer' => $log_entry['referer'], - 'hostname' => drupal_substr($log_entry['ip'], 0, 128), - 'timestamp' => $log_entry['timestamp'], - )) - ->execute(); + try { + Database::getConnection('default', 'default')->insert('watchdog') + ->fields(array( + 'uid' => $log_entry['uid'], + 'type' => drupal_substr($log_entry['type'], 0, 64), + 'message' => $log_entry['message'], + 'variables' => serialize($log_entry['variables']), + 'severity' => $log_entry['severity'], + 'link' => drupal_substr($log_entry['link'], 0, 255), + 'location' => $log_entry['request_uri'], + 'referer' => $log_entry['referer'], + 'hostname' => drupal_substr($log_entry['ip'], 0, 128), + 'timestamp' => $log_entry['timestamp'], + )) + ->execute(); + } + catch (Exception $e) { + // Exception is ignored so that watchdog does not break pages during the + // installation process or is not able to create the watchdog table during + // installation. + } } /** diff --git a/modules/dblog/dblog.test b/modules/dblog/dblog.test index a233d971bfa..ad07688526c 100644 --- a/modules/dblog/dblog.test +++ b/modules/dblog/dblog.test @@ -520,6 +520,33 @@ class DBLogTestCase extends DrupalWebTestCase { $this->assertText(t('Database log cleared.'), 'Confirmation message found'); } + /** + * Verifies that exceptions are caught in dblog_watchdog(). + */ + protected function testDBLogException() { + $log = array( + 'type' => 'custom', + 'message' => 'Log entry added to test watchdog handling of Exceptions.', + 'variables' => array(), + 'severity' => WATCHDOG_NOTICE, + 'link' => NULL, + 'user' => $this->big_user, + 'uid' => isset($this->big_user->uid) ? $this->big_user->uid : 0, + 'request_uri' => request_uri(), + 'referer' => $_SERVER['HTTP_REFERER'], + 'ip' => ip_address(), + 'timestamp' => REQUEST_TIME, + ); + + // Remove watchdog table temporarily to simulate it missing during + // installation. + db_query("DROP TABLE {watchdog}"); + + // Add a watchdog entry. + // This should not throw an Exception, but fail silently. + dblog_watchdog($log); + } + /** * Gets the database log event information from the browser page. *