dblog.module 6.6 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
Dries's avatar
 
Dries committed
2

Dries's avatar
 
Dries committed
3 4 5 6
/**
 * @file
 * System monitoring and logging for administrators.
 *
7
 * The dblog module monitors your site and keeps a list of
Dries's avatar
 
Dries committed
8 9 10
 * recorded events containing usage and performance data, errors,
 * warnings, and similar operational information.
 *
11
 * @see watchdog()
Dries's avatar
 
Dries committed
12 13
 */

Dries's avatar
Dries committed
14
/**
15
 * Implements hook_help().
Dries's avatar
Dries committed
16
 */
17 18
function dblog_help($path, $arg) {
  switch ($path) {
19
    case 'admin/help#dblog':
20 21 22 23 24 25
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Database logging module logs system events in the Drupal database. For more information, see the online handbook entry for the <a href="@dblog">Database logging module</a>.', array('@dblog' => 'http://drupal.org/handbook/modules/dblog')) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Monitoring your site') . '</dt>';
26
      $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.', array('@dblog' => url('admin/reports/dblog'))) . '</dd>';
27
      $output .= '<dt>' . t('Debugging site problems') . '</dt>';
28
      $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.', array('@dblog' => url('admin/reports/dblog'))) . '</dd>';
29
      $output .= '</dl>';
30
      return $output;
31
    case 'admin/reports/dblog':
32
      return '<p>' . t('The Database logging module monitors your website, capturing system events in a log (shown here) to be reviewed by an authorized individual at a later time. This log is a list of recorded events containing usage data, performance data, errors, warnings and operational information. It is vital to check the Recent log messages report on a regular basis, as it is often the only way to tell what is going on.') . '</p>';
Dries's avatar
 
Dries committed
33
  }
34 35
}

Dries's avatar
 
Dries committed
36
/**
37
 * Implements hook_menu().
Dries's avatar
 
Dries committed
38
 */
39
function dblog_menu() {
40
  $items['admin/reports/dblog'] = array(
41
    'title' => 'Recent log messages',
42
    'description' => 'View events that have recently been logged.',
43
    'page callback' => 'dblog_overview',
44
    'access arguments' => array('access site reports'),
45
    'weight' => -1,
46
    'file' => 'dblog.admin.inc',
47
  );
48
  $items['admin/reports/page-not-found'] = array(
49 50
    'title' => "Top 'page not found' errors",
    'description' => "View 'page not found' errors (404s).",
51
    'page callback' => 'dblog_top',
52
    'page arguments' => array('page not found'),
53
    'access arguments' => array('access site reports'),
54
    'file' => 'dblog.admin.inc',
55
  );
56
  $items['admin/reports/access-denied'] = array(
57
    'title' => "Top 'access denied' errors",
58
    'description' => "View 'access denied' errors (403s).",
59
    'page callback' => 'dblog_top',
60
    'page arguments' => array('access denied'),
61
    'access arguments' => array('access site reports'),
62
    'file' => 'dblog.admin.inc',
63
  );
64
  $items['admin/reports/event/%'] = array(
65
    'title' => 'Details',
66
    'page callback' => 'dblog_event',
67
    'page arguments' => array(3),
68
    'access arguments' => array('access site reports'),
69
    'file' => 'dblog.admin.inc',
70
  );
71 72 73 74 75 76 77 78 79 80 81 82

  if (module_exists('search')) {
    $items['admin/reports/search'] = array(
      'title' => 'Top search phrases',
      'description' => 'View most popular search phrases.',
      'page callback' => 'dblog_top',
      'page arguments' => array('search'),
      'access arguments' => array('access site reports'),
      'file' => 'dblog.admin.inc',
    );
  }

83 84
  return $items;
}
Dries's avatar
 
Dries committed
85

86 87 88
/**
 * Implements hook_init().
 */
89
function dblog_init() {
90
  if (arg(0) == 'admin' && arg(1) == 'reports') {
91
    // Add the CSS for this module
92
    drupal_add_css(drupal_get_path('module', 'dblog') . '/dblog.css');
93
  }
Dries's avatar
 
Dries committed
94 95
}

Dries's avatar
Dries committed
96
/**
97
 * Implements hook_cron().
Dries's avatar
Dries committed
98
 *
Dries's avatar
 
Dries committed
99
 * Remove expired log messages and flood control events.
Dries's avatar
Dries committed
100
 */
101
function dblog_cron() {
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  // Cleanup the watchdog table.
  $row_limit = variable_get('dblog_row_limit', 1000);

  // For row limit n, get the wid of the nth row in descending wid order.
  // Counting the most recent n rows avoids issues with wid number sequences,
  // e.g. auto_increment value > 1 or rows deleted directly from the table.
  if ($row_limit > 0) {
    $min_row = db_select('watchdog', 'w')
      ->fields('w', array('wid'))
      ->orderBy('wid', 'DESC')
      ->range($row_limit - 1, 1)
      ->execute()->fetchField();

    // Delete all table entries older than the nth row, if nth row was found.
    if ($min_row) {
      db_delete('watchdog')
        ->condition('wid', $min_row, '<')
        ->execute();
    }
121
  }
Dries's avatar
 
Dries committed
122
}
Dries's avatar
 
Dries committed
123

124
function _dblog_get_message_types() {
Dries's avatar
 
Dries committed
125 126
  $types = array();

127
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
128
  foreach ($result as $object) {
Dries's avatar
 
Dries committed
129 130 131 132 133
    $types[] = $object->type;
  }

  return $types;
}
134

135
/**
136
 * Implements hook_watchdog().
137 138 139 140
 *
 * Note some values may be truncated for database column size restrictions.
 */
function dblog_watchdog(array $log_entry) {
141
  Database::getConnection('default', 'default')->insert('watchdog')
142
    ->fields(array(
143 144 145 146 147 148 149 150 151 152
      'uid' => $log_entry['user']->uid,
      'type' => substr($log_entry['type'], 0, 64),
      'message' => $log_entry['message'],
      'variables' => serialize($log_entry['variables']),
      'severity' => $log_entry['severity'],
      'link' => substr($log_entry['link'], 0, 255),
      'location' => $log_entry['request_uri'],
      'referer' => $log_entry['referer'],
      'hostname' => substr($log_entry['ip'], 0, 128),
      'timestamp' => $log_entry['timestamp'],
153 154
    ))
    ->execute();
155
}
156

157
/**
158
 * Implements hook_form_FORM_ID_alter().
159 160 161 162
 */
function dblog_form_system_logging_settings_alter(&$form, $form_state) {
  $form['dblog_row_limit'] = array(
    '#type' => 'select',
163
    '#title' => t('Database log messages to keep'),
164
    '#default_value' => variable_get('dblog_row_limit', 1000),
165
    '#options' => array(0 => t('All')) + drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
166
    '#description' => t('The maximum number of messages to keep in the database log. Requires a <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status')))
167
  );
168
  $form['actions']['#weight'] = 1;
169
}
170 171 172 173 174 175 176 177 178 179 180 181

/**
 * Implements hook_theme().
 */
function dblog_theme() {
  return array(
    'dblog_message' => array(
      'variables' => array('event' => NULL, 'link' => FALSE),
      'file' => 'dblog.admin.inc',
    ),
  );
}