dblog.module 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Form\FormStateInterface;
  11. use Drupal\Core\Routing\RouteMatchInterface;
  12. use Drupal\Core\StringTranslation\TranslatableMarkup;
  13. /**
  14. * Implements hook_help().
  15. */
  16. function dblog_help($route_name, RouteMatchInterface $route_match) {
  17. switch ($route_name) {
  18. case 'help.page.dblog':
  19. $output = '';
  20. $output .= '<h3>' . t('About') . '</h3>';
  21. $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>';
  22. $output .= '<h3>' . t('Uses') . '</h3>';
  23. $output .= '<dl>';
  24. $output .= '<dt>' . t('Monitoring your site') . '</dt>';
  25. $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>';
  26. $output .= '<dt>' . t('Debugging site problems') . '</dt>';
  27. $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>';
  28. $output .= '</dl>';
  29. return $output;
  30. case 'dblog.overview':
  31. 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>';
  32. }
  33. }
  34. /**
  35. * Implements hook_menu_links_discovered_alter().
  36. */
  37. function dblog_menu_links_discovered_alter(&$links) {
  38. if (\Drupal::moduleHandler()->moduleExists('search')) {
  39. $links['dblog.search'] = [
  40. 'title' => new TranslatableMarkup('Top search phrases'),
  41. 'route_name' => 'dblog.search',
  42. 'description' => new TranslatableMarkup('View most popular search phrases.'),
  43. 'parent' => 'system.admin_reports',
  44. ];
  45. }
  46. return $links;
  47. }
  48. /**
  49. * Implements hook_cron().
  50. *
  51. * Controls the size of the log table, paring it to 'dblog_row_limit' messages.
  52. */
  53. function dblog_cron() {
  54. // Cleanup the watchdog table.
  55. $row_limit = \Drupal::config('dblog.settings')->get('row_limit');
  56. // For row limit n, get the wid of the nth row in descending wid order.
  57. // Counting the most recent n rows avoids issues with wid number sequences,
  58. // e.g. auto_increment value > 1 or rows deleted directly from the table.
  59. if ($row_limit > 0) {
  60. $min_row = db_select('watchdog', 'w')
  61. ->fields('w', ['wid'])
  62. ->orderBy('wid', 'DESC')
  63. ->range($row_limit - 1, 1)
  64. ->execute()->fetchField();
  65. // Delete all table entries older than the nth row, if nth row was found.
  66. if ($min_row) {
  67. db_delete('watchdog')
  68. ->condition('wid', $min_row, '<')
  69. ->execute();
  70. }
  71. }
  72. }
  73. /**
  74. * Gathers a list of uniquely defined database log message types.
  75. *
  76. * @return array
  77. * List of uniquely defined database log message types.
  78. */
  79. function _dblog_get_message_types() {
  80. return db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type')
  81. ->fetchAllKeyed(0, 0);
  82. }
  83. /**
  84. * Implements hook_form_FORM_ID_alter() for system_logging_settings().
  85. */
  86. function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) {
  87. $row_limits = [100, 1000, 10000, 100000, 1000000];
  88. $form['dblog_row_limit'] = [
  89. '#type' => 'select',
  90. '#title' => t('Database log messages to keep'),
  91. '#default_value' => \Drupal::configFactory()->getEditable('dblog.settings')->get('row_limit'),
  92. '#options' => [0 => t('All')] + array_combine($row_limits, $row_limits),
  93. '#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')])
  94. ];
  95. $form['#submit'][] = 'dblog_logging_settings_submit';
  96. }
  97. /**
  98. * Form submission handler for system_logging_settings().
  99. *
  100. * @see dblog_form_system_logging_settings_alter()
  101. */
  102. function dblog_logging_settings_submit($form, FormStateInterface $form_state) {
  103. \Drupal::configFactory()->getEditable('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save();
  104. }