From 3311fc67975cc099b395352abf2a5fbdf1ad026c Mon Sep 17 00:00:00 2001 From: Frederic G. MARAND Date: Wed, 25 Apr 2012 22:10:47 +0200 Subject: [PATCH] Issue #1268636 by fgm: Ignore watchdog() calls below the system reporting level. --- core/includes/bootstrap.inc | 7 ++- core/modules/system/system.admin.inc | 29 ++++++++- core/modules/system/system.test | 66 ++++++++++++++++++++ .../tests/modules/watchdog_test/watchdog_test.info | 6 ++ .../modules/watchdog_test/watchdog_test.module | 8 +++ 5 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 core/modules/system/tests/modules/watchdog_test/watchdog_test.info create mode 100644 core/modules/system/tests/modules/watchdog_test/watchdog_test.module diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 6abe08a..d158d9f 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -1552,7 +1552,7 @@ function watchdog_exception($type, Exception $exception, $message = NULL, $varia } /** - * Logs a system message. + * Logs a system message if its severity level warrants it. * * @param $type * The category to which this message belongs. Can be any string, but the @@ -1578,6 +1578,7 @@ function watchdog_exception($type, Exception $exception, $message = NULL, $varia * - WATCHDOG_NOTICE: (default) Normal but significant conditions. * - WATCHDOG_INFO: Informational messages. * - WATCHDOG_DEBUG: Debug-level messages. + * Events less severe than the current limit will be ignored. * @param $link * A link to associate with the message. * @@ -1592,6 +1593,10 @@ function watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NO // It is possible that the error handling will itself trigger an error. In that case, we could // end up in an infinite loop. To avoid that, we implement a simple static semaphore. if (!$in_error_state && function_exists('module_implements')) { + if ($severity > variable_get('watchdog_limit', WATCHDOG_DEBUG)) { + return; + } + $in_error_state = TRUE; // The user object may not exist in all conditions, so 0 is substituted if needed. diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index aac0c3d..86b0575 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -1631,14 +1631,41 @@ function system_logging_settings() { '#description' => t('It is recommended that sites running on production environments do not display any errors.'), ); + $config = config('system.logging'); + + // Default to WATCHDOG_DEBUG for safety, not performance. + $form['watchdog_limit'] = array( + '#type' => 'select', + '#title' => t('Minimum logging level'), + '#description' => t('Any watchdog event less important than that level will be filtered out and not send to watchdog implementations. It is recommended that production sites do not log debug events.'), + '#options' => watchdog_severity_levels(), + '#default_value' => $config->get('watchdog_limit') ?: WATCHDOG_DEBUG, + ); + + $form['#submit'][] = 'system_logging_settings_submit'; + return system_settings_form($form); } /** + * Save config settings and prevent their being saved to variables. + * + * @ingroup forms + * @see system_settings_form_submit() + */ +function system_logging_settings_submit($form, &$form_state) { + $config = config('system.logging'); + $config->set('watchdog_limit', $form_state['values']['watchdog_limit']); + $config->save(); + + unset($form_state['values']['watchdog_limit']); +} + +/** * Form builder; Configure site performance settings. * * @ingroup forms - * @see system_performance_settings_submit(). + * @see system_performance_settings_submit() */ function system_performance_settings($form, &$form_state) { drupal_add_js(drupal_get_path('module', 'system') . '/system.js'); diff --git a/core/modules/system/system.test b/core/modules/system/system.test index 16348cc..daa8586 100644 --- a/core/modules/system/system.test +++ b/core/modules/system/system.test @@ -2903,3 +2903,69 @@ class SystemIndexPhpTest extends DrupalWebTestCase { $this->assertResponse(404, t("Make sure index.php/user returns a 'page not found'.")); } } + +/** + * Test the behaviour of watchdog() without depending on dblog.module. + */ +class WatchdogTest extends DrupalWebTestCase { + public $module = 'watchdog_test'; + public $function = 'watchdog_test_watchdog'; + + /** + * Override the default "standard" profile for this test, we need to include + * a test-specific module, which should not be present on a standard install. + * + * @var string + */ + protected $profile = 'testing'; + + public static function getInfo() { + return array( + 'name' => 'Watchdog_limit observance', + 'description' => 'Make sure watchdog() applies the watchdog_limit variable', + 'group' => 'Bootstrap', + ); + } + + function setUp() { + parent::setUp('watchdog_test'); + } + + function assertEntry($message) { + $logged = variable_get($this->function, NULL); + return $this->assertEqual($logged, $message, t('Event is logged and message is unchanged')); + } + + function assertNoEntry($message) { + $logged = variable_get($this->function, NULL); + return $this->assertNull($logged, t('Event is not logged.')); + } + /** + * Test the default and non-default behaviours + */ + function testWatchdogLimit() { + $message = $this->randomString(32); + $this->pass(t('Logging %message', array('%message' => $message))); + // Default limit should be WATCHDOG_DEBUG + $this->assertEqual(variable_get('watchdog_limit', WATCHDOG_DEBUG), WATCHDOG_DEBUG, + t('watchdog_limit defaults to WATCHDOG_DEBUG')); + variable_del($this->function); + watchdog($this->module, $message, NULL, WATCHDOG_DEBUG); + $this->assertEntry($message); + + // Now request a higher level: unimportant events should be ignored... + variable_del($this->function); + variable_set('watchdog_limit', WATCHDOG_NOTICE); + watchdog($this->module, $message, NULL, WATCHDOG_DEBUG); + $this->assertNoEntry($message); + + // ... but events at the limit or more important should be logged + variable_del($this->function); + watchdog($this->module, $message, NULL, WATCHDOG_NOTICE); + $this->assertEntry($message); + + variable_del($this->function); + watchdog($this->module, $message, NULL, WATCHDOG_ERROR); + $this->assertEntry($message); + } +} diff --git a/core/modules/system/tests/modules/watchdog_test/watchdog_test.info b/core/modules/system/tests/modules/watchdog_test/watchdog_test.info new file mode 100644 index 0000000..6d48507 --- /dev/null +++ b/core/modules/system/tests/modules/watchdog_test/watchdog_test.info @@ -0,0 +1,6 @@ +name = "Watchdog limit test" +description = "Support module for testing the watchdog function." +package = Testing +version = VERSION +core = 8.x +hidden = TRUE diff --git a/core/modules/system/tests/modules/watchdog_test/watchdog_test.module b/core/modules/system/tests/modules/watchdog_test/watchdog_test.module new file mode 100644 index 0000000..bd3e0b5 --- /dev/null +++ b/core/modules/system/tests/modules/watchdog_test/watchdog_test.module @@ -0,0 +1,8 @@ +