Commit 04a25239 authored by webchick's avatar webchick

Issue #1493108 by marcingy, Mike Wacker, sun, kbasarab, Berdir, Rok Žlender,...

Issue #1493108 by marcingy, Mike Wacker, sun, kbasarab, Berdir, Rok Žlender, cosmicdreams, alexpott: Convert logging and error settings to configuration system.
parent 28385827
...@@ -10,22 +10,22 @@ ...@@ -10,22 +10,22 @@
/** /**
* Error reporting level: display no errors. * Error reporting level: display no errors.
*/ */
const ERROR_REPORTING_HIDE = 0; const ERROR_REPORTING_HIDE = 'hide';
/** /**
* Error reporting level: display errors and warnings. * Error reporting level: display errors and warnings.
*/ */
const ERROR_REPORTING_DISPLAY_SOME = 1; const ERROR_REPORTING_DISPLAY_SOME = 'some';
/** /**
* Error reporting level: display all messages. * Error reporting level: display all messages.
*/ */
const ERROR_REPORTING_DISPLAY_ALL = 2; const ERROR_REPORTING_DISPLAY_ALL = 'all';
/** /**
* Error reporting level: display all messages, plus backtrace information. * Error reporting level: display all messages, plus backtrace information.
*/ */
const ERROR_REPORTING_DISPLAY_VERBOSE = 3; const ERROR_REPORTING_DISPLAY_VERBOSE = 'verbose';
/** /**
* Maps PHP error constants to watchdog severity levels. * Maps PHP error constants to watchdog severity levels.
...@@ -172,7 +172,7 @@ function _drupal_render_exception_safe($exception) { ...@@ -172,7 +172,7 @@ function _drupal_render_exception_safe($exception) {
* TRUE if an error should be displayed. * TRUE if an error should be displayed.
*/ */
function error_displayable($error = NULL) { function error_displayable($error = NULL) {
$error_level = variable_get('error_level', ERROR_REPORTING_DISPLAY_ALL); $error_level = config('system.logging')->get('error_level');
$updating = (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update'); $updating = (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update');
$all_errors_displayed = ($error_level == ERROR_REPORTING_DISPLAY_ALL) || $all_errors_displayed = ($error_level == ERROR_REPORTING_DISPLAY_ALL) ||
($error_level == ERROR_REPORTING_DISPLAY_VERBOSE); ($error_level == ERROR_REPORTING_DISPLAY_VERBOSE);
......
...@@ -92,8 +92,21 @@ function dblog_schema() { ...@@ -92,8 +92,21 @@ function dblog_schema() {
} }
/** /**
* Implements hook_uninstall(). * @defgroup updates-7.x-to-8.x Updates from 7.x to 8.x
* @{
* Update functions from 7.x to 8.x.
*/ */
function dblog_uninstall() {
variable_del('dblog_row_limit'); /**
* Update settings to the new configuration system.
*/
function dblog_update_8000() {
update_variables_to_config('dblog.settings', array(
'dblog_row_limit' => 'row_limit',
));
} }
/**
* @} End of "defgroup updates-7.x-to-8.x"
* The next series of updates should start at 9000.
*/
...@@ -102,7 +102,7 @@ function dblog_init() { ...@@ -102,7 +102,7 @@ function dblog_init() {
*/ */
function dblog_cron() { function dblog_cron() {
// Cleanup the watchdog table. // Cleanup the watchdog table.
$row_limit = variable_get('dblog_row_limit', 1000); $row_limit = config('dblog.settings')->get('row_limit');
// For row limit n, get the wid of the nth row in descending wid order. // For row limit n, get the wid of the nth row in descending wid order.
// Counting the most recent n rows avoids issues with wid number sequences, // Counting the most recent n rows avoids issues with wid number sequences,
...@@ -163,11 +163,21 @@ function dblog_form_system_logging_settings_alter(&$form, $form_state) { ...@@ -163,11 +163,21 @@ function dblog_form_system_logging_settings_alter(&$form, $form_state) {
$form['dblog_row_limit'] = array( $form['dblog_row_limit'] = array(
'#type' => 'select', '#type' => 'select',
'#title' => t('Database log messages to keep'), '#title' => t('Database log messages to keep'),
'#default_value' => variable_get('dblog_row_limit', 1000), '#default_value' => config('dblog.settings')->get('row_limit'),
'#options' => array(0 => t('All')) + drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)), '#options' => array(0 => t('All')) + drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
'#description' => t('The maximum number of messages to keep in the database log. Requires a <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status'))) '#description' => t('The maximum number of messages to keep in the database log. Requires a <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status')))
); );
$form['actions']['#weight'] = 1;
$form['#submit'][] = 'dblog_logging_settings_submit';
}
/**
* Form submission handler for system_logging_settings().
*
* @see dblog_form_system_logging_settings_alter()
*/
function dblog_logging_settings_submit($form, &$form_state) {
config('dblog.settings')->set('row_limit', $form_state['values']['dblog_row_limit'])->save();
} }
/** /**
......
...@@ -65,11 +65,8 @@ private function verifyRowLimit($row_limit) { ...@@ -65,11 +65,8 @@ private function verifyRowLimit($row_limit) {
$this->assertResponse(200); $this->assertResponse(200);
// Check row limit variable. // Check row limit variable.
$current_limit = variable_get('dblog_row_limit', 1000); $current_limit = config('dblog.settings')->get('row_limit');
$this->assertTrue($current_limit == $row_limit, t('[Cache] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit))); $this->assertTrue($current_limit == $row_limit, t('[Cache] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit)));
// Verify dblog row limit equals specified row limit.
$current_limit = unserialize(db_query("SELECT value FROM {variable} WHERE name = :dblog_limit", array(':dblog_limit' => 'dblog_row_limit'))->fetchField());
$this->assertTrue($current_limit == $row_limit, t('[Variable table] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit)));
} }
/** /**
......
identity: drupal
facility: ''
format: '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'
...@@ -6,10 +6,32 @@ ...@@ -6,10 +6,32 @@
*/ */
/** /**
* Implements hook_uninstall(). * Implements hook_install().
*/ */
function syslog_uninstall() { function syslog_install() {
variable_del('syslog_identity'); // The default facility setting depends on the operating system, so it needs
variable_del('syslog_facility'); // to be set dynamically during installation.
variable_del('syslog_format'); config('syslog.settings')->set('facility', defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER)->save();
} }
/**
* @defgroup updates-7.x-to-8.x Updates from 7.x to 8.x
* @{
* Update functions from 7.x to 8.x.
*/
/**
* Update settings to the new configuration system.
**/
function syslog_update_8000() {
update_variables_to_config('syslog.settings', array(
'syslog_identity' => 'identity',
'syslog_facility' => 'facility',
'syslog_format' => 'format',
));
}
/**
* @} End of "defgroup updates-7.x-to-8.x"
* The next series of updates should start at 9000.
*/
...@@ -45,18 +45,19 @@ function syslog_help($path, $arg) { ...@@ -45,18 +45,19 @@ function syslog_help($path, $arg) {
* Implements hook_form_FORM_ID_alter(). * Implements hook_form_FORM_ID_alter().
*/ */
function syslog_form_system_logging_settings_alter(&$form, &$form_state) { function syslog_form_system_logging_settings_alter(&$form, &$form_state) {
$config = config('syslog.settings');
$help = module_exists('help') ? ' ' . l(t('More information'), 'admin/help/syslog') . '.' : NULL; $help = module_exists('help') ? ' ' . l(t('More information'), 'admin/help/syslog') . '.' : NULL;
$form['syslog_identity'] = array( $form['syslog_identity'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#title' => t('Syslog identity'), '#title' => t('Syslog identity'),
'#default_value' => variable_get('syslog_identity', 'drupal'), '#default_value' => $config->get('identity'),
'#description' => t('A string that will be prepended to every message logged to Syslog. If you have multiple sites logging to the same Syslog log file, a unique identity per site makes it easy to tell the log entries apart.') . $help, '#description' => t('A string that will be prepended to every message logged to Syslog. If you have multiple sites logging to the same Syslog log file, a unique identity per site makes it easy to tell the log entries apart.') . $help,
); );
if (defined('LOG_LOCAL0')) { if (defined('LOG_LOCAL0')) {
$form['syslog_facility'] = array( $form['syslog_facility'] = array(
'#type' => 'select', '#type' => 'select',
'#title' => t('Syslog facility'), '#title' => t('Syslog facility'),
'#default_value' => variable_get('syslog_facility', LOG_LOCAL0), '#default_value' => $config->get('facility'),
'#options' => syslog_facility_list(), '#options' => syslog_facility_list(),
'#description' => t('Depending on the system configuration, Syslog and other logging tools use this code to identify or filter messages from within the entire system log.') . $help, '#description' => t('Depending on the system configuration, Syslog and other logging tools use this code to identify or filter messages from within the entire system log.') . $help,
); );
...@@ -64,10 +65,24 @@ function syslog_form_system_logging_settings_alter(&$form, &$form_state) { ...@@ -64,10 +65,24 @@ function syslog_form_system_logging_settings_alter(&$form, &$form_state) {
$form['syslog_format'] = array( $form['syslog_format'] = array(
'#type' => 'textarea', '#type' => 'textarea',
'#title' => t('Syslog format'), '#title' => t('Syslog format'),
'#default_value' => variable_get('syslog_format', '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'), '#default_value' => $config->get('format'),
'#description' => t('Specify the format of the syslog entry. Available variables are: <dl><dt><code>!base_url</code></dt><dd>Base URL of the site.</dd><dt><code>!timestamp</code></dt><dd>Unix timestamp of the log entry.</dd><dt><code>!type</code></dt><dd>The category to which this message belongs.</dd><dt><code>!ip</code></dt><dd>IP address of the user triggering the message.</dd><dt><code>!request_uri</code></dt><dd>The requested URI.</dd><dt><code>!referer</code></dt><dd>HTTP Referer if available.</dd><dt><code>!uid</code></dt><dd>User ID.</dd><dt><code>!link</code></dt><dd>A link to associate with the message.</dd><dt><code>!message</code></dt><dd>The message to store in the log.</dd></dl>'), '#description' => t('Specify the format of the syslog entry. Available variables are: <dl><dt><code>!base_url</code></dt><dd>Base URL of the site.</dd><dt><code>!timestamp</code></dt><dd>Unix timestamp of the log entry.</dd><dt><code>!type</code></dt><dd>The category to which this message belongs.</dd><dt><code>!ip</code></dt><dd>IP address of the user triggering the message.</dd><dt><code>!request_uri</code></dt><dd>The requested URI.</dd><dt><code>!referer</code></dt><dd>HTTP Referer if available.</dd><dt><code>!uid</code></dt><dd>User ID.</dd><dt><code>!link</code></dt><dd>A link to associate with the message.</dd><dt><code>!message</code></dt><dd>The message to store in the log.</dd></dl>'),
); );
$form['actions']['#weight'] = 1;
$form['#submit'][] = 'syslog_logging_settings_submit';
}
/**
* Form submission handler for system_logging_settings().
*
* @see syslog_form_system_logging_settings_alter()
*/
function syslog_logging_settings_submit($form, &$form_state) {
config('syslog.settings')
->set('identity', $form_state['values']['syslog_identity'])
->set('facility', $form_state['values']['syslog_facility'])
->set('format', $form_state['values']['syslog_format'])
->save();
} }
/** /**
...@@ -95,14 +110,18 @@ function syslog_watchdog(array $log_entry) { ...@@ -95,14 +110,18 @@ function syslog_watchdog(array $log_entry) {
global $base_url; global $base_url;
$log_init = &drupal_static(__FUNCTION__, FALSE); $log_init = &drupal_static(__FUNCTION__, FALSE);
$config = config('syslog.settings');
if (!$log_init) { if (!$log_init) {
$log_init = TRUE; $log_init = TRUE;
$default_facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER; $facility = $config->get('facility');
openlog(variable_get('syslog_identity', 'drupal'), LOG_NDELAY, variable_get('syslog_facility', $default_facility)); if ($facility === '') {
$facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER;
}
openlog($config->get('identity'), LOG_NDELAY, $facility);
} }
$message = strtr(variable_get('syslog_format', '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'), array( $message = strtr($config->get('format'), array(
'!base_url' => $base_url, '!base_url' => $base_url,
'!timestamp' => $log_entry['timestamp'], '!timestamp' => $log_entry['timestamp'],
'!type' => $log_entry['type'], '!type' => $log_entry['type'],
......
...@@ -274,7 +274,7 @@ function testDrupalRenderInvalidKeys() { ...@@ -274,7 +274,7 @@ function testDrupalRenderInvalidKeys() {
); );
$message = t('%type: !message in %function (line ', $error); $message = t('%type: !message in %function (line ', $error);
variable_set('error_level', ERROR_REPORTING_DISPLAY_ALL); config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
$this->drupalGet('common-test/drupal-render-invalid-keys'); $this->drupalGet('common-test/drupal-render-invalid-keys');
$this->assertResponse(200, t('Received expected HTTP status code.')); $this->assertResponse(200, t('Received expected HTTP status code.'));
$this->assertRaw($message, t('Found error message: !message.', array('!message' => $message))); $this->assertRaw($message, t('Found error message: !message.', array('!message' => $message)));
......
...@@ -29,6 +29,7 @@ function setUp() { ...@@ -29,6 +29,7 @@ function setUp() {
* Test the error handler. * Test the error handler.
*/ */
function testErrorHandler() { function testErrorHandler() {
$config = config('system.logging');
$error_notice = array( $error_notice = array(
'%type' => 'Notice', '%type' => 'Notice',
'!message' => 'Undefined variable: bananas', '!message' => 'Undefined variable: bananas',
...@@ -49,7 +50,7 @@ function testErrorHandler() { ...@@ -49,7 +50,7 @@ function testErrorHandler() {
); );
// Set error reporting to collect notices. // Set error reporting to collect notices.
variable_set('error_level', ERROR_REPORTING_DISPLAY_ALL); $config->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
$this->drupalGet('error-test/generate-warnings'); $this->drupalGet('error-test/generate-warnings');
$this->assertResponse(200, t('Received expected HTTP status code.')); $this->assertResponse(200, t('Received expected HTTP status code.'));
$this->assertErrorMessage($error_notice); $this->assertErrorMessage($error_notice);
...@@ -57,7 +58,7 @@ function testErrorHandler() { ...@@ -57,7 +58,7 @@ function testErrorHandler() {
$this->assertErrorMessage($error_user_notice); $this->assertErrorMessage($error_user_notice);
// Set error reporting to not collect notices. // Set error reporting to not collect notices.
variable_set('error_level', ERROR_REPORTING_DISPLAY_SOME); $config->set('error_level', ERROR_REPORTING_DISPLAY_SOME)->save();
$this->drupalGet('error-test/generate-warnings'); $this->drupalGet('error-test/generate-warnings');
$this->assertResponse(200, t('Received expected HTTP status code.')); $this->assertResponse(200, t('Received expected HTTP status code.'));
$this->assertNoErrorMessage($error_notice); $this->assertNoErrorMessage($error_notice);
...@@ -65,7 +66,7 @@ function testErrorHandler() { ...@@ -65,7 +66,7 @@ function testErrorHandler() {
$this->assertErrorMessage($error_user_notice); $this->assertErrorMessage($error_user_notice);
// Set error reporting to not show any errors. // Set error reporting to not show any errors.
variable_set('error_level', ERROR_REPORTING_HIDE); $config->set('error_level', ERROR_REPORTING_HIDE)->save();
$this->drupalGet('error-test/generate-warnings'); $this->drupalGet('error-test/generate-warnings');
$this->assertResponse(200, t('Received expected HTTP status code.')); $this->assertResponse(200, t('Received expected HTTP status code.'));
$this->assertNoErrorMessage($error_notice); $this->assertNoErrorMessage($error_notice);
......
...@@ -1646,13 +1646,13 @@ function system_run_cron_submit($form, &$form_state) { ...@@ -1646,13 +1646,13 @@ function system_run_cron_submit($form, &$form_state) {
* Form builder; Configure error reporting settings. * Form builder; Configure error reporting settings.
* *
* @ingroup forms * @ingroup forms
* @see system_settings_form() * @see system_logging_settings_submit()
*/ */
function system_logging_settings() { function system_logging_settings($form, &$form_state) {
$form['error_level'] = array( $form['error_level'] = array(
'#type' => 'radios', '#type' => 'radios',
'#title' => t('Error messages to display'), '#title' => t('Error messages to display'),
'#default_value' => variable_get('error_level', ERROR_REPORTING_DISPLAY_ALL), '#default_value' => config('system.logging')->get('error_level'),
'#options' => array( '#options' => array(
ERROR_REPORTING_HIDE => t('None'), ERROR_REPORTING_HIDE => t('None'),
ERROR_REPORTING_DISPLAY_SOME => t('Errors and warnings'), ERROR_REPORTING_DISPLAY_SOME => t('Errors and warnings'),
...@@ -1662,7 +1662,18 @@ function system_logging_settings() { ...@@ -1662,7 +1662,18 @@ function system_logging_settings() {
'#description' => t('It is recommended that sites running on production environments do not display any errors.'), '#description' => t('It is recommended that sites running on production environments do not display any errors.'),
); );
return system_settings_form($form); return system_config_form($form, $form_state);
}
/**
* Form submission handler for system_logging_settings().
*
* @ingroup forms
*/
function system_logging_settings_submit($form, &$form_state) {
config('system.logging')
->set('error_level', $form_state['values']['error_level'])
->save();
} }
/** /**
......
...@@ -1971,6 +1971,32 @@ function system_update_8013() { ...@@ -1971,6 +1971,32 @@ function system_update_8013() {
} }
} }
/**
* Moves system logging settings from variables to config.
*/
function system_update_8014() {
$error_level = db_query("SELECT value FROM {variable} WHERE name = 'error_level'")->fetchField();
$config = config('system.logging');
// Only do the conversion if we have a value and it is numeric.
if ($error_level && is_numeric($error_level)) {
$map = array(
'0' => 'hide',
'1' => 'some',
'2' => 'all',
'3' => 'verbose',
);
// Update error_level value to a string identifier.
$config->set('error_level', $map[$error_level]);
// Delete the migrated variable.
db_delete('variable')->condition('name', 'error_level')->execute();
}
else {
// Update error_level to the default value.
$config->set('error_level', 'all');
}
$config->save();
}
/** /**
* @} End of "defgroup updates-7.x-to-8.x". * @} End of "defgroup updates-7.x-to-8.x".
* The next series of updates should start at 9000. * The next series of updates should start at 9000.
......
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