annotate core/modules/dblog/dblog.module @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * System monitoring and logging for administrators.
Chris@0 6 *
Chris@0 7 * The Database Logging module monitors your site and keeps a list of recorded
Chris@0 8 * events containing usage and performance data, errors, warnings, and similar
Chris@0 9 * operational information.
Chris@0 10 */
Chris@0 11
Chris@0 12 use Drupal\Core\Form\FormStateInterface;
Chris@0 13 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 14 use Drupal\Core\StringTranslation\TranslatableMarkup;
Chris@0 15 use Drupal\views\ViewEntityInterface;
Chris@0 16 use Drupal\views\ViewExecutable;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Implements hook_help().
Chris@0 20 */
Chris@0 21 function dblog_help($route_name, RouteMatchInterface $route_match) {
Chris@0 22 switch ($route_name) {
Chris@0 23 case 'help.page.dblog':
Chris@0 24 $output = '';
Chris@0 25 $output .= '<h3>' . t('About') . '</h3>';
Chris@0 26 $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>';
Chris@0 27 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 28 $output .= '<dl>';
Chris@0 29 $output .= '<dt>' . t('Monitoring your site') . '</dt>';
Chris@0 30 $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>';
Chris@0 31 $output .= '<dt>' . t('Debugging site problems') . '</dt>';
Chris@0 32 $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>';
Chris@0 33 $output .= '</dl>';
Chris@0 34 return $output;
Chris@0 35
Chris@0 36 case 'dblog.overview':
Chris@0 37 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>';
Chris@0 38 }
Chris@0 39 }
Chris@0 40
Chris@0 41 /**
Chris@0 42 * Implements hook_menu_links_discovered_alter().
Chris@0 43 */
Chris@0 44 function dblog_menu_links_discovered_alter(&$links) {
Chris@0 45 if (\Drupal::moduleHandler()->moduleExists('search')) {
Chris@0 46 $links['dblog.search'] = [
Chris@0 47 'title' => new TranslatableMarkup('Top search phrases'),
Chris@0 48 'route_name' => 'dblog.search',
Chris@0 49 'description' => new TranslatableMarkup('View most popular search phrases.'),
Chris@0 50 'parent' => 'system.admin_reports',
Chris@0 51 ];
Chris@0 52 }
Chris@0 53
Chris@0 54 return $links;
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * Implements hook_cron().
Chris@0 59 *
Chris@0 60 * Controls the size of the log table, paring it to 'dblog_row_limit' messages.
Chris@0 61 */
Chris@0 62 function dblog_cron() {
Chris@0 63 // Cleanup the watchdog table.
Chris@0 64 $row_limit = \Drupal::config('dblog.settings')->get('row_limit');
Chris@0 65
Chris@0 66 // For row limit n, get the wid of the nth row in descending wid order.
Chris@0 67 // Counting the most recent n rows avoids issues with wid number sequences,
Chris@0 68 // e.g. auto_increment value > 1 or rows deleted directly from the table.
Chris@0 69 if ($row_limit > 0) {
Chris@0 70 $min_row = db_select('watchdog', 'w')
Chris@0 71 ->fields('w', ['wid'])
Chris@0 72 ->orderBy('wid', 'DESC')
Chris@0 73 ->range($row_limit - 1, 1)
Chris@0 74 ->execute()->fetchField();
Chris@0 75
Chris@0 76 // Delete all table entries older than the nth row, if nth row was found.
Chris@0 77 if ($min_row) {
Chris@0 78 db_delete('watchdog')
Chris@0 79 ->condition('wid', $min_row, '<')
Chris@0 80 ->execute();
Chris@0 81 }
Chris@0 82 }
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * Gathers a list of uniquely defined database log message types.
Chris@0 87 *
Chris@0 88 * @return array
Chris@0 89 * List of uniquely defined database log message types.
Chris@0 90 */
Chris@0 91 function _dblog_get_message_types() {
Chris@0 92 return db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type')
Chris@0 93 ->fetchAllKeyed(0, 0);
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@0 97 * Implements hook_form_FORM_ID_alter() for system_logging_settings().
Chris@0 98 */
Chris@0 99 function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) {
Chris@0 100 $row_limits = [100, 1000, 10000, 100000, 1000000];
Chris@0 101 $form['dblog_row_limit'] = [
Chris@0 102 '#type' => 'select',
Chris@0 103 '#title' => t('Database log messages to keep'),
Chris@0 104 '#default_value' => \Drupal::configFactory()->getEditable('dblog.settings')->get('row_limit'),
Chris@0 105 '#options' => [0 => t('All')] + array_combine($row_limits, $row_limits),
Chris@0 106 '#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')])
Chris@0 107 ];
Chris@0 108
Chris@0 109 $form['#submit'][] = 'dblog_logging_settings_submit';
Chris@0 110 }
Chris@0 111
Chris@0 112 /**
Chris@0 113 * Form submission handler for system_logging_settings().
Chris@0 114 *
Chris@0 115 * @see dblog_form_system_logging_settings_alter()
Chris@0 116 */
Chris@0 117 function dblog_logging_settings_submit($form, FormStateInterface $form_state) {
Chris@0 118 \Drupal::configFactory()->getEditable('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save();
Chris@0 119 }
Chris@0 120
Chris@0 121 /**
Chris@0 122 * Implements hook_ENTITY_TYPE_presave() for views entities.
Chris@0 123 *
Chris@0 124 * This hook ensures there are no views based that are using a wrong plugin for
Chris@0 125 * wid and uid fields on views that use watchdog as base table.
Chris@0 126 *
Chris@0 127 * @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
Chris@0 128 *
Chris@0 129 * @see https://www.drupal.org/node/2876378
Chris@0 130 */
Chris@0 131 function dblog_view_presave(ViewEntityInterface $view) {
Chris@0 132 // Only interested in watchdog based views.
Chris@0 133 if ($view->get('base_table') != 'watchdog') {
Chris@0 134 return;
Chris@0 135 }
Chris@0 136
Chris@0 137 $displays = $view->get('display');
Chris@0 138 $update = FALSE;
Chris@0 139 foreach ($displays as $display_name => $display) {
Chris@0 140
Chris@0 141 // Iterate through all the fields of watchdog views based tables.
Chris@0 142 if (isset($display['display_options']['fields'])) {
Chris@0 143 foreach ($display['display_options']['fields'] as $field_name => $field) {
Chris@0 144 // We are only interested in wid and uid fields from the watchdog table
Chris@0 145 // that still use the numeric id.
Chris@0 146 if (isset($field['table']) &&
Chris@0 147 $field['table'] === 'watchdog' &&
Chris@0 148 $field['plugin_id'] == 'numeric' &&
Chris@0 149 in_array($field['field'], ['wid', 'uid'], TRUE)) {
Chris@0 150
Chris@0 151 $displays[$display_name]['display_options']['fields'][$field_name]['plugin_id'] = 'standard';
Chris@0 152
Chris@0 153 // Delete all the attributes related to numeric fields.
Chris@0 154 unset(
Chris@0 155 $displays[$display_name]['display_options']['fields'][$field_name]['set_precision'],
Chris@0 156 $displays[$display_name]['display_options']['fields'][$field_name]['precision'],
Chris@0 157 $displays[$display_name]['display_options']['fields'][$field_name]['decimal'],
Chris@0 158 $displays[$display_name]['display_options']['fields'][$field_name]['separator'],
Chris@0 159 $displays[$display_name]['display_options']['fields'][$field_name]['format_plural'],
Chris@0 160 $displays[$display_name]['display_options']['fields'][$field_name]['format_plural_string'],
Chris@0 161 $displays[$display_name]['display_options']['fields'][$field_name]['prefix'],
Chris@0 162 $displays[$display_name]['display_options']['fields'][$field_name]['suffix']
Chris@0 163 );
Chris@0 164
Chris@0 165 $update = TRUE;
Chris@0 166 @trigger_error("The numeric plugin for watchdog.$field_name field is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Must use standard plugin instead. See https://www.drupal.org/node/2876378.", E_USER_DEPRECATED);
Chris@0 167 }
Chris@0 168 }
Chris@0 169 }
Chris@0 170
Chris@0 171 // Iterate all filters looking for type filters to update.
Chris@0 172 if (isset($display['display_options']['filters'])) {
Chris@0 173 foreach ($display['display_options']['filters'] as $filter_name => $filter) {
Chris@0 174 if (isset($filter['table']) &&
Chris@0 175 $filter['table'] === 'watchdog' &&
Chris@0 176 $filter['plugin_id'] == 'in_operator' &&
Chris@0 177 $filter['field'] == 'type') {
Chris@0 178
Chris@0 179 $displays[$display_name]['display_options']['filters'][$filter_name]['plugin_id'] = 'dblog_types';
Chris@0 180 $update = TRUE;
Chris@0 181 @trigger_error("The in_operator plugin for watchdog.type filter is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Must use dblog_types plugin instead. See https://www.drupal.org/node/2876378.", E_USER_DEPRECATED);
Chris@0 182 }
Chris@0 183 }
Chris@0 184 }
Chris@0 185 }
Chris@0 186
Chris@0 187 if ($update) {
Chris@0 188 $view->set('display', $displays);
Chris@0 189 }
Chris@0 190 }
Chris@0 191
Chris@0 192 /**
Chris@0 193 * Implements hook_views_pre_render().
Chris@0 194 */
Chris@0 195 function dblog_views_pre_render(ViewExecutable $view) {
Chris@0 196 if (isset($view) && ($view->storage->get('base_table') == 'watchdog')) {
Chris@0 197 $view->element['#attached']['library'][] = 'dblog/drupal.dblog';
Chris@0 198 }
Chris@0 199 }