5 * System monitoring and logging for administrators.
7 * The Database Logging module monitors your site and keeps a list of recorded
8 * events containing usage and performance data, errors, warnings, and similar
9 * operational information.
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Routing\RouteMatchInterface;
14 use Drupal\Core\StringTranslation\TranslatableMarkup;
17 * Implements hook_help().
19 function dblog_help($route_name, RouteMatchInterface $route_match) {
20 switch ($route_name) {
21 case 'help.page.dblog':
23 $output .= '<h3>' . t('About') . '</h3>';
24 $output .= '<p>' . t('The Database Logging module logs system events in the Drupal database. For more information, see the <a href=":dblog">online documentation for the Database Logging module</a>.', [':dblog' => 'https://www.drupal.org/documentation/modules/dblog']) . '</p>';
25 $output .= '<h3>' . t('Uses') . '</h3>';
27 $output .= '<dt>' . t('Monitoring your site') . '</dt>';
28 $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.', [':dblog' => \Drupal::url('dblog.overview')]) . '</dd>';
29 $output .= '<dt>' . t('Debugging site problems') . '</dt>';
30 $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.', [':dblog' => \Drupal::url('dblog.overview')]) . '</dd>';
34 case 'dblog.overview':
35 return '<p>' . t('The Database Logging module logs system events in the Drupal database. Monitor your site or debug site problems on this page.') . '</p>';
40 * Implements hook_menu_links_discovered_alter().
42 function dblog_menu_links_discovered_alter(&$links) {
43 if (\Drupal::moduleHandler()->moduleExists('search')) {
44 $links['dblog.search'] = [
45 'title' => new TranslatableMarkup('Top search phrases'),
46 'route_name' => 'dblog.search',
47 'description' => new TranslatableMarkup('View most popular search phrases.'),
48 'parent' => 'system.admin_reports',
56 * Implements hook_cron().
58 * Controls the size of the log table, paring it to 'dblog_row_limit' messages.
60 function dblog_cron() {
61 // Cleanup the watchdog table.
62 $row_limit = \Drupal::config('dblog.settings')->get('row_limit');
64 // For row limit n, get the wid of the nth row in descending wid order.
65 // Counting the most recent n rows avoids issues with wid number sequences,
66 // e.g. auto_increment value > 1 or rows deleted directly from the table.
68 $min_row = db_select('watchdog', 'w')
69 ->fields('w', ['wid'])
70 ->orderBy('wid', 'DESC')
71 ->range($row_limit - 1, 1)
72 ->execute()->fetchField();
74 // Delete all table entries older than the nth row, if nth row was found.
77 ->condition('wid', $min_row, '<')
84 * Gathers a list of uniquely defined database log message types.
87 * List of uniquely defined database log message types.
89 function _dblog_get_message_types() {
90 return db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type')
91 ->fetchAllKeyed(0, 0);
95 * Implements hook_form_FORM_ID_alter() for system_logging_settings().
97 function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) {
98 $row_limits = [100, 1000, 10000, 100000, 1000000];
99 $form['dblog_row_limit'] = [
101 '#title' => t('Database log messages to keep'),
102 '#default_value' => \Drupal::configFactory()->getEditable('dblog.settings')->get('row_limit'),
103 '#options' => [0 => t('All')] + array_combine($row_limits, $row_limits),
104 '#description' => t('The maximum number of messages to keep in the database log. Requires a <a href=":cron">cron maintenance task</a>.', [':cron' => \Drupal::url('system.status')])
107 $form['#submit'][] = 'dblog_logging_settings_submit';
111 * Form submission handler for system_logging_settings().
113 * @see dblog_form_system_logging_settings_alter()
115 function dblog_logging_settings_submit($form, FormStateInterface $form_state) {
116 \Drupal::configFactory()->getEditable('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save();