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;
15 use Drupal\views\ViewEntityInterface;
16 use Drupal\views\ViewExecutable;
19 * Implements hook_help().
21 function dblog_help($route_name, RouteMatchInterface $route_match) {
22 switch ($route_name) {
23 case 'help.page.dblog':
25 $output .= '<h3>' . t('About') . '</h3>';
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>';
27 $output .= '<h3>' . t('Uses') . '</h3>';
29 $output .= '<dt>' . t('Monitoring your site') . '</dt>';
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>';
31 $output .= '<dt>' . t('Debugging site problems') . '</dt>';
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>';
36 case 'dblog.overview':
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>';
42 * Implements hook_menu_links_discovered_alter().
44 function dblog_menu_links_discovered_alter(&$links) {
45 if (\Drupal::moduleHandler()->moduleExists('search')) {
46 $links['dblog.search'] = [
47 'title' => new TranslatableMarkup('Top search phrases'),
48 'route_name' => 'dblog.search',
49 'description' => new TranslatableMarkup('View most popular search phrases.'),
50 'parent' => 'system.admin_reports',
58 * Implements hook_cron().
60 * Controls the size of the log table, paring it to 'dblog_row_limit' messages.
62 function dblog_cron() {
63 // Cleanup the watchdog table.
64 $row_limit = \Drupal::config('dblog.settings')->get('row_limit');
66 // For row limit n, get the wid of the nth row in descending wid order.
67 // Counting the most recent n rows avoids issues with wid number sequences,
68 // e.g. auto_increment value > 1 or rows deleted directly from the table.
70 $min_row = db_select('watchdog', 'w')
71 ->fields('w', ['wid'])
72 ->orderBy('wid', 'DESC')
73 ->range($row_limit - 1, 1)
74 ->execute()->fetchField();
76 // Delete all table entries older than the nth row, if nth row was found.
79 ->condition('wid', $min_row, '<')
86 * Gathers a list of uniquely defined database log message types.
89 * List of uniquely defined database log message types.
91 function _dblog_get_message_types() {
92 return db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type')
93 ->fetchAllKeyed(0, 0);
97 * Implements hook_form_FORM_ID_alter() for system_logging_settings().
99 function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) {
100 $row_limits = [100, 1000, 10000, 100000, 1000000];
101 $form['dblog_row_limit'] = [
103 '#title' => t('Database log messages to keep'),
104 '#default_value' => \Drupal::configFactory()->getEditable('dblog.settings')->get('row_limit'),
105 '#options' => [0 => t('All')] + array_combine($row_limits, $row_limits),
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')])
109 $form['#submit'][] = 'dblog_logging_settings_submit';
113 * Form submission handler for system_logging_settings().
115 * @see dblog_form_system_logging_settings_alter()
117 function dblog_logging_settings_submit($form, FormStateInterface $form_state) {
118 \Drupal::configFactory()->getEditable('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save();
122 * Implements hook_ENTITY_TYPE_presave() for views entities.
124 * This hook ensures there are no views based that are using a wrong plugin for
125 * wid and uid fields on views that use watchdog as base table.
127 * @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
129 * @see https://www.drupal.org/node/2876378
131 function dblog_view_presave(ViewEntityInterface $view) {
132 // Only interested in watchdog based views.
133 if ($view->get('base_table') != 'watchdog') {
137 $displays = $view->get('display');
139 foreach ($displays as $display_name => $display) {
141 // Iterate through all the fields of watchdog views based tables.
142 if (isset($display['display_options']['fields'])) {
143 foreach ($display['display_options']['fields'] as $field_name => $field) {
144 // We are only interested in wid and uid fields from the watchdog table
145 // that still use the numeric id.
146 if (isset($field['table']) &&
147 $field['table'] === 'watchdog' &&
148 $field['plugin_id'] == 'numeric' &&
149 in_array($field['field'], ['wid', 'uid'], TRUE)) {
151 $displays[$display_name]['display_options']['fields'][$field_name]['plugin_id'] = 'standard';
153 // Delete all the attributes related to numeric fields.
155 $displays[$display_name]['display_options']['fields'][$field_name]['set_precision'],
156 $displays[$display_name]['display_options']['fields'][$field_name]['precision'],
157 $displays[$display_name]['display_options']['fields'][$field_name]['decimal'],
158 $displays[$display_name]['display_options']['fields'][$field_name]['separator'],
159 $displays[$display_name]['display_options']['fields'][$field_name]['format_plural'],
160 $displays[$display_name]['display_options']['fields'][$field_name]['format_plural_string'],
161 $displays[$display_name]['display_options']['fields'][$field_name]['prefix'],
162 $displays[$display_name]['display_options']['fields'][$field_name]['suffix']
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);
171 // Iterate all filters looking for type filters to update.
172 if (isset($display['display_options']['filters'])) {
173 foreach ($display['display_options']['filters'] as $filter_name => $filter) {
174 if (isset($filter['table']) &&
175 $filter['table'] === 'watchdog' &&
176 $filter['plugin_id'] == 'in_operator' &&
177 $filter['field'] == 'type') {
179 $displays[$display_name]['display_options']['filters'][$filter_name]['plugin_id'] = 'dblog_types';
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);
188 $view->set('display', $displays);
193 * Implements hook_views_pre_render().
195 function dblog_views_pre_render(ViewExecutable $view) {
196 if (isset($view) && ($view->storage->get('base_table') == 'watchdog')) {
197 $view->element['#attached']['library'][] = 'dblog/drupal.dblog';