dblog.module 8.3 KB

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