drupal/core/modules/dblog/dblog.module

133 lines
5.1 KiB
Plaintext

<?php
/**
* @file
* System monitoring and logging for administrators.
*
* The Database Logging module monitors your site and keeps a list of recorded
* events containing usage and performance data, errors, warnings, and similar
* operational information.
*
* @see watchdog()
*/
use Symfony\Component\HttpFoundation\Request;
/**
* Implements hook_help().
*/
function dblog_help($route_name, Request $request) {
switch ($route_name) {
case 'help.page.dblog':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Database Logging module logs system events in the Drupal database. For more information, see the online handbook entry for the <a href="!dblog">Database Logging module</a>.', array('!dblog' => 'http://drupal.org/documentation/modules/dblog')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Monitoring your site') . '</dt>';
$output .= '<dd>' . t('The Database Logging module allows you to view an event log on the <a href="!dblog">Recent log messages</a> page. The log is a chronological list of recorded events containing usage data, performance data, errors, warnings and operational information. Administrators should check the log on a regular basis to ensure their site is working properly.', array('!dblog' => \Drupal::url('dblog.overview'))) . '</dd>';
$output .= '<dt>' . t('Debugging site problems') . '</dt>';
$output .= '<dd>' . t('In case of errors or problems with the site, the <a href="!dblog">Recent log messages</a> page can be useful for debugging, since it shows the sequence of events. The log messages include usage information, warnings, and errors.', array('!dblog' => \Drupal::url('dblog.overview'))) . '</dd>';
$output .= '</dl>';
return $output;
case 'dblog.overview':
return '<p>' . t('The Database Logging module monitors your website, capturing system events in a log (shown here) to be reviewed by an authorized individual at a later time. This log is a list of recorded events containing usage data, performance data, errors, warnings and operational information. It is vital to check the Recent log messages report on a regular basis, as it is often the only way to tell what is going on.') . '</p>';
}
}
/**
* Implements hook_menu_link_defaults_alter().
*/
function dblog_menu_link_defaults_alter(&$links) {
if (\Drupal::moduleHandler()->moduleExists('search')) {
$links['dblog.search'] = array(
'title' => 'Top search phrases',
'route_name' => 'dblog.search',
'description' => 'View most popular search phrases.',
'parent' => 'system.admin_reports',
);
}
return $links;
}
/**
* Implements hook_page_build().
*/
function dblog_page_build(&$page) {
if (arg(0) == 'admin' && arg(1) == 'reports') {
$page['#attached']['css'][] = drupal_get_path('module', 'dblog') . '/css/dblog.module.css';
}
}
/**
* Implements hook_cron().
*
* Controls the size of the log table, paring it to 'dblog_row_limit' messages.
*/
function dblog_cron() {
// Cleanup the watchdog table.
$row_limit = \Drupal::config('dblog.settings')->get('row_limit');
// 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,
// e.g. auto_increment value > 1 or rows deleted directly from the table.
if ($row_limit > 0) {
$min_row = db_select('watchdog', 'w')
->fields('w', array('wid'))
->orderBy('wid', 'DESC')
->range($row_limit - 1, 1)
->execute()->fetchField();
// Delete all table entries older than the nth row, if nth row was found.
if ($min_row) {
db_delete('watchdog')
->condition('wid', $min_row, '<')
->execute();
}
}
}
/**
* Gathers a list of uniquely defined database log message types.
*
* @return array
* List of uniquely defined database log message types.
*/
function _dblog_get_message_types() {
$types = array();
$result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
foreach ($result as $object) {
$types[] = $object->type;
}
return array_combine($types, $types);
}
/**
* Implements hook_form_FORM_ID_alter() for system_logging_settings().
*/
function dblog_form_system_logging_settings_alter(&$form, $form_state) {
$row_limits = array(100, 1000, 10000, 100000, 1000000);
$form['dblog_row_limit'] = array(
'#type' => 'select',
'#title' => t('Database log messages to keep'),
'#default_value' => \Drupal::config('dblog.settings')->get('row_limit'),
'#options' => array(0 => t('All')) + array_combine($row_limits, $row_limits),
'#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['#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) {
\Drupal::config('dblog.settings')->set('row_limit', $form_state['values']['dblog_row_limit'])->save();
}