dblog.module 5.98 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
 * Implement 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
      $output = '<p>' . t('The dblog module monitors your system, capturing system events in a log to be reviewed by an authorized individual at a later time. This is useful for site administrators who want a quick overview of activities on their site. The logs also record the sequence of events, so it can be useful for debugging site errors.') . '</p>';
      $output .= '<p>' . t('The dblog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information. Administrators should check the dblog report on a regular basis to ensure their site is working properly.') . '</p>';
      $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@dblog">Dblog module</a>.', array('@dblog' => 'http://drupal.org/handbook/modules/dblog/')) . '</p>';
24
      return $output;
25
    case 'admin/reports/dblog':
26
      return '<p>' . t('The dblog module monitors your website, capturing system events in a log to be reviewed by an authorized individual at a later time. The dblog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information. It is vital to check the dblog report on a regular basis as it is often the only way to tell what is going on.') . '</p>';
Dries's avatar
 
Dries committed
27
  }
28 29
}

30
/**
31
 * Implement hook_theme().
32
 */
33
function dblog_theme() {
34
  return array(
35
    'dblog_filters' => array(
36 37 38 39 40
      'arguments' => array('form' => NULL),
    ),
  );
}

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

80
function dblog_init() {
81
  if (arg(0) == 'admin' && arg(1) == 'reports') {
82
    // Add the CSS for this module
83
    drupal_add_css(drupal_get_path('module', 'dblog') . '/dblog.css', array('preprocess' => FALSE));
84
  }
Dries's avatar
 
Dries committed
85 86
}

87 88


Dries's avatar
Dries committed
89
/**
90
 * Implement hook_cron().
Dries's avatar
Dries committed
91
 *
Dries's avatar
 
Dries committed
92
 * Remove expired log messages and flood control events.
Dries's avatar
Dries committed
93
 */
94 95
function dblog_cron() {
  // Cleanup the watchdog table
96 97 98 99
  $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
100
}
Dries's avatar
 
Dries committed
101

102
/**
103
 * Implement hook_user_cancel().
104
 */
105
function dblog_user_cancel($edit, $account, $method) {
106 107
  switch ($method) {
    case 'user_cancel_reassign':
108 109 110 111
      db_update('watchdog')
        ->fields(array('uid' => 0))
        ->condition('uid', $account->uid)
        ->execute();
112 113 114
      break;

    case 'user_cancel_delete':
115 116 117
      db_delete('watchdog')
        ->condition('uid', $account->uid)
        ->execute();
118 119
      break;
  }
120 121
}

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

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

  return $types;
}
132

133
/**
134
 * Implement hook_watchdog().
135 136 137 138
 *
 * Note some values may be truncated for database column size restrictions.
 */
function dblog_watchdog(array $log_entry) {
139
  Database::getConnection('default', 'default')->insert('watchdog')
140
    ->fields(array(
141 142 143 144 145 146 147 148 149 150
      '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'],
151 152
    ))
    ->execute();
153
}
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168
/**
 * Implement hook_form_FORM_ID_alter().
 */
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),
    '#options' => drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
    '#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;
}

169 170
/**
 * Theme dblog administration filter selector.
171 172
 *
 * @ingroup themeable
173 174 175 176 177 178
 */
function theme_dblog_filters($form) {
  $output = '';
  foreach (element_children($form['status']) as $key) {
    $output .= drupal_render($form['status'][$key]);
  }
179
  $output .= '<div id="dblog-admin-buttons">' . drupal_render($form['buttons']) . '</div>';
180 181
  return $output;
}