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

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

Dries's avatar
Dries committed
15
/**
16
 * Implements hook_help().
Dries's avatar
Dries committed
17
 */
18 19
function dblog_help($path, $arg) {
  switch ($path) {
20
    case 'admin/help#dblog':
21 22 23 24 25 26 27 28 29 30 31
      $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>';
      $output .= '<dd>' . t('The Database logging module allows you to view an event log on the <a href="@dblog">Recent log entries</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>';
      $output .= '<dt>' . t('Debugging site problems') . '</dt>';
      $output .= '<dd>' . t('In case of errors or problems with the site, the <a href="@dblog">Recent log entries</a> page can be useful for debugging, since it shows the sequence of events. The log entries include usage information, warnings, and errors.', array('@dblog' => url('admin/reports/dblog'))) . '</dd>';
      $output .= '</dl>';
      return $output;	
32
    case 'admin/reports/dblog':
33
      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 entries report on a regular basis, as it is often the only way to tell what is going on.') . '</p>';
Dries's avatar
 
Dries committed
34
  }
35 36
}

37
/**
38
 * Implements hook_theme().
39
 */
40
function dblog_theme() {
41
  return array(
42
    'dblog_filters' => array(
43
      'render element' => 'form',
44 45 46 47
    ),
  );
}

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

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

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 102
function dblog_cron() {
  // Cleanup the watchdog table
103 104 105 106 107 108
  if (variable_get('dblog_row_limit', 1000) > 0) {
    $max = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
    db_delete('watchdog')
      ->condition('wid', $max - variable_get('dblog_row_limit', 1000), '<=')
      ->execute();
  }
Dries's avatar
 
Dries committed
109
}
Dries's avatar
 
Dries committed
110

111
/**
112
 * Implements hook_user_cancel().
113
 */
114
function dblog_user_cancel($edit, $account, $method) {
115 116
  switch ($method) {
    case 'user_cancel_reassign':
117 118 119 120
      db_update('watchdog')
        ->fields(array('uid' => 0))
        ->condition('uid', $account->uid)
        ->execute();
121 122 123
      break;

    case 'user_cancel_delete':
124 125 126
      db_delete('watchdog')
        ->condition('uid', $account->uid)
        ->execute();
127 128
      break;
  }
129 130
}

131
function _dblog_get_message_types() {
Dries's avatar
 
Dries committed
132 133
  $types = array();

134
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
135
  foreach ($result as $object) {
Dries's avatar
 
Dries committed
136 137 138 139 140
    $types[] = $object->type;
  }

  return $types;
}
141

142
/**
143
 * Implements hook_watchdog().
144 145 146 147
 *
 * Note some values may be truncated for database column size restrictions.
 */
function dblog_watchdog(array $log_entry) {
148
  Database::getConnection('default', 'default')->insert('watchdog')
149
    ->fields(array(
150 151 152 153 154 155 156 157 158 159
      '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'],
160 161
    ))
    ->execute();
162
}
163

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

178 179
/**
 * Theme dblog administration filter selector.
180 181
 *
 * @ingroup themeable
182
 */
183 184
function theme_dblog_filters($variables) {
  $form = $variables['form'];
185
  $output = '';
186

187 188 189
  foreach (element_children($form['status']) as $key) {
    $output .= drupal_render($form['status'][$key]);
  }
190
  $output .= '<div id="dblog-admin-buttons">' . drupal_render($form['buttons']) . '</div>';
191 192
  return $output;
}