dblog.module 5.15 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 12 13 14
 * recorded events containing usage and performance data, errors,
 * warnings, and similar operational information.
 *
 * @see watchdog().
 */

Dries's avatar
Dries committed
15 16 17
/**
 * Implementation of hook_help().
 */
18 19
function dblog_help($path, $arg) {
  switch ($path) {
20 21 22 23
    case 'admin/help#dblog':
      $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 please read the configuration and customization handbook <a href="@dblog">Dblog page</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 32
/**
 * Implementation of hook_theme()
 */
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
/**
Dries's avatar
 
Dries committed
42
 * Implementation of hook_menu().
Dries's avatar
 
Dries committed
43
 */
44 45
function dblog_menu() {
  $items['admin/settings/logging/dblog'] = array(
46 47
    'title' => 'Database logging',
    'description' => 'Settings for logging to the Drupal database logs. This is the most common method for small to medium sites on shared hosting. The logs are viewable from the admin pages.',
48 49
    'page callback' => 'drupal_get_form',
    'page arguments' => array('dblog_admin_settings'),
50
    'file' => 'dblog.admin.inc',
51 52
  );

53
  $items['admin/reports/dblog'] = array(
54 55
    'title' => 'Recent log entries',
    'description' => 'View events that have recently been logged.',
56
    'page callback' => 'dblog_overview',
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
    'file' => 'dblog.admin.inc',
66
  );
67
  $items['admin/reports/access-denied'] = array(
68 69
      'title' => "Top 'access denied' errors",
    'description' => "View 'access denied' errors (403s).",
70
    'page callback' => 'dblog_top',
71
    'page arguments' => array('access denied'),
72
    'file' => 'dblog.admin.inc',
73
  );
74
  $items['admin/reports/event/%'] = array(
75
    'title' => 'Details',
76
    'page callback' => 'dblog_event',
77 78
    'page arguments' => array(3),
    'type' => MENU_CALLBACK,
79
    'file' => 'dblog.admin.inc',
80 81 82
  );
  return $items;
}
Dries's avatar
 
Dries committed
83

84
function dblog_init() {
85 86
  if (arg(0) == 'admin' && arg(1) == 'logs') {
    // Add the CSS for this module
87
    drupal_add_css(drupal_get_path('module', 'dblog') .'/dblog.css', 'module', 'all', FALSE);
88
  }
Dries's avatar
 
Dries committed
89 90
}

91 92


Dries's avatar
Dries committed
93 94 95
/**
 * Implementation of hook_cron().
 *
Dries's avatar
 
Dries committed
96
 * Remove expired log messages and flood control events.
Dries's avatar
Dries committed
97
 */
98 99
function dblog_cron() {
  // Cleanup the watchdog table
100 101
  $max = db_result(db_query('SELECT MAX(wid) FROM {watchdog}'));
  db_query('DELETE FROM {watchdog} WHERE wid < %d', $max - variable_get('dblog_row_limit', 1000));
Dries's avatar
 
Dries committed
102
}
Dries's avatar
 
Dries committed
103

104 105 106
/**
 * Implementation of hook_user().
 */
107
function dblog_user($op, &$edit, &$user) {
108
  if ($op == 'delete') {
Steven Wittens's avatar
Steven Wittens committed
109
    db_query('UPDATE {watchdog} SET uid = 0 WHERE uid = %d', $user->uid);
110 111 112
  }
}

113
function _dblog_get_message_types() {
Dries's avatar
 
Dries committed
114 115
  $types = array();

116
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
Dries's avatar
 
Dries committed
117 118 119 120 121 122
  while ($object = db_fetch_object($result)) {
    $types[] = $object->type;
  }

  return $types;
}
123 124 125 126

function dblog_watchdog($log = array()) {
  $current_db = db_set_active();
  db_query("INSERT INTO {watchdog}
127
    (uid, type, message, variables, severity, link, location, referer, hostname, timestamp)
128
    VALUES
129
    (%d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', %d)",
130 131 132
    $log['user']->uid,
    $log['type'],
    $log['message'],
133
    serialize($log['variables']),
134 135 136 137 138 139 140 141 142 143 144
    $log['severity'],
    $log['link'],
    $log['request_uri'],
    $log['referer'],
    $log['ip'],
    $log['timestamp']);

  if ($current_db) {
    db_set_active($current_db);
  }
}
145

146 147 148 149 150 151 152 153 154 155 156 157
/**
 * Theme dblog administration filter selector.
 */
function theme_dblog_filters($form) {
  $output = '';
  foreach (element_children($form['status']) as $key) {
    $output .= drupal_render($form['status'][$key]);
  }
  $output .= '<div id="dblog-admin-buttons">'. drupal_render($form['buttons']) .'</div>';
  return $output;
}