From a98c4164d65a4a669d41c0815996c9ccfd58c6b3 Mon Sep 17 00:00:00 2001
From: Frederic G. MARAND <fgm@osinet.fr>
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                        |    8 ++++++-
 core/modules/system/config/system.logging.yml      |    2 +
 core/modules/system/system.admin.inc               |   22 +++++++++++++++++--
 .../tests/modules/watchdog_test/watchdog_test.info |    6 +++++
 .../modules/watchdog_test/watchdog_test.module     |   10 +++++++++
 5 files changed, 44 insertions(+), 4 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 43cc27e..8b45738 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -1645,7 +1645,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
@@ -1670,6 +1670,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.
  *
@@ -1684,6 +1685,11 @@ function watchdog($type, $message, array $variables = array(), $severity = WATCH
   // 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')) {
+    $config = config('system.logging');
+    if ($severity > $config->get('watchdog_limit')) {
+      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/config/system.logging.yml b/core/modules/system/config/system.logging.yml
index 3ecc76c..6d1f0fe 100644
--- a/core/modules/system/config/system.logging.yml
+++ b/core/modules/system/config/system.logging.yml
@@ -1 +1,3 @@
 error_level: all
+watchdog_limit: 7
+
diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc
index 72c50d4..28fbba5 100644
--- a/core/modules/system/system.admin.inc
+++ b/core/modules/system/system.admin.inc
@@ -1662,10 +1662,12 @@ function system_run_cron_submit($form, &$form_state) {
  * @see system_logging_settings_submit()
  */
 function system_logging_settings($form, &$form_state) {
+  $config = config('system.logging');
+
   $form['error_level'] = array(
     '#type' => 'radios',
     '#title' => t('Error messages to display'),
-    '#default_value' => config('system.logging')->get('error_level'),
+    '#default_value' => $config->get('error_level'),
     '#options' => array(
       ERROR_REPORTING_HIDE => t('None'),
       ERROR_REPORTING_DISPLAY_SOME => t('Errors and warnings'),
@@ -1675,6 +1677,15 @@ function system_logging_settings($form, &$form_state) {
     '#description' => t('It is recommended that sites running on production environments do not display any errors.'),
   );
 
+  // 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'),
+  );
+
   return system_config_form($form, $form_state);
 }
 
@@ -1684,16 +1695,21 @@ function system_logging_settings($form, &$form_state) {
  * @ingroup forms
  */
 function system_logging_settings_submit($form, &$form_state) {
-  config('system.logging')
+  $config = config('system.logging');
+  $config
     ->set('error_level', $form_state['values']['error_level'])
     ->save();
+
+  $config
+    ->set('watchdog_limit', $form_state['values']['watchdog_limit'])
+    ->save();
 }
 
 /**
  * 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/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..dafec5f
--- /dev/null
+++ b/core/modules/system/tests/modules/watchdog_test/watchdog_test.module
@@ -0,0 +1,10 @@
+<?php
+
+/**
+ * Implements hook_watchdog().
+ *
+ * Do not use config, this is state information, not configuration.
+ */
+function watchdog_test_watchdog(array $log_entry) {
+  variable_set(__FUNCTION__, $log_entry['message']);
+}
\ No newline at end of file
-- 
1.7.4.1

