annotate core/modules/dblog/dblog.module @ 19:fa3358dc1485 tip

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