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

Dries's avatar
 
Dries committed
4
function watchdog_help($section = "admin/help#watchdog") {
Dries's avatar
 
Dries committed
5
  $output = "";
Dries's avatar
 
Dries committed
6 7

  switch ($section) {
Dries's avatar
 
Dries committed
8
    case 'admin/help#watchdog':
Dries's avatar
 
Dries committed
9 10
      $output .= t("
      <p>Watchdog module monitors your web site, capturing system events in a log to be reviewed by an authorized individual at a later time.  The watchdog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information.  It is vital to <a href=\"%watchdog\">check the watchdog report</a> on a regular basis as it is often the only way to tell what is going on.</p>
Dries's avatar
 
Dries committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
      <p>To ease administration, the watchdog will automatically discard old log entries, <a href=\"%log-entry\">as configured</a>. Needs \"cron.php\" to discard the entries.</p>", array("%watchdog" => url("admin/logs"), "%log-entry" => url("admin/system/modules/watchdog")));
      break;
    case 'admin/logs':
      $output = t("The watchdog module monitors your web site, captures system events in a log and records them to be reviewed by an authorized individual at a later time.  The watchdog log is simply a list of events recorded during operation and contains usage data, performance data, errors, warnings and operational information. It is vital to check the watchdog report on a regular basis as it is often the only way to tell what is going on.");
      break;
    case 'admin/logs/user':
       $output = t("Watchdog events that have to do with users.");
       break;
    case 'admin/logs/regular':
      $output = t("Watchdog events that are \"normal\" and have no other classification.");
      break;
    case 'admin/logs/httpd':
      $output = t("Watchdog events that are from the web server.");
      break;
    case 'admin/logs/special':
      $output = t("Watchdog events about adding, changing, and moderating nodes and comments.");
      break;
    case 'admin/logs/search':
      $output = t("Watchdog events showing what users searched for.");
      break;
    case 'admin/logs/error':
      $output = t("Watchdog events about PHP and database errors.");
      break;
    case 'admin/logs/warning':
      $output = t("Watchdog warning events. These events don't stop Drupal from running, but are things you should know.");
      break;
    case 'admin/system/modules#description':
      $output = t("Logs and records system events.");
      break;
    case 'admin/system/modules/watchdog':
      $output = t("Watchdog logs your system events.  To see these events go to the <a href=\"%watchdog\">logs</a>. Since these logs can grow out of control if kept around forever, below set how long an item should be kept in the log.  Note that to discard entries as set below you must run \"cron.php\" regularly.", array("%watchdog" => url("admin/logs")));
Dries's avatar
 
Dries committed
42 43
      break;
  }
Dries's avatar
 
Dries committed
44

Dries's avatar
 
Dries committed
45
  return $output;
46 47
}

Dries's avatar
 
Dries committed
48
function watchdog_perm() {
Dries's avatar
 
Dries committed
49
  return array("administer watchdog");
Dries's avatar
 
Dries committed
50 51
}

Dries's avatar
 
Dries committed
52 53 54
/**
 * Implementation of hook_link().
 */
Dries's avatar
 
Dries committed
55
function watchdog_link($type) {
Dries's avatar
 
Dries committed
56 57 58 59
  if ($type == 'system') {
    menu('admin/logs', t('logs'), user_access('administer watchdog') ? 'watchdog_admin' : MENU_DENIED, 7);
    menu('admin/logs/view', t('view details'), user_access('administer watchdog') ? 'watchdog_admin' : MENU_DENIED, 0, MENU_HIDE, MENU_LOCKED);

Kjartan's avatar
Kjartan committed
60 61 62 63
    if (arg(1) == 'logs') {
      foreach (_watchdog_get_message_types() as $type) {
        menu("admin/logs/$type", t($type), user_access('administer watchdog') ? 'watchdog_admin' : MENU_DENIED, 0, MENU_SHOW, MENU_LOCKED);
      }
Dries's avatar
 
Dries committed
64
    }
Dries's avatar
 
Dries committed
65 66 67
  }
}

68
function watchdog_settings() {
Dries's avatar
 
Dries committed
69 70
  $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200), "format_interval");
  $period[1000000000] = t('Never');
Dries's avatar
 
Dries committed
71
  $output .= form_select(t("Discard entries older than"), "watchdog_clear", variable_get("watchdog_clear", 604800), $period, t("The time watchdog entries should be kept.  Older entries will be automatically discarded.  Requires crontab."));
Dries's avatar
 
Dries committed
72 73 74
  return $output;
}

Dries's avatar
 
Dries committed
75
function watchdog_cron() {
Dries's avatar
 
Dries committed
76
  db_query("DELETE FROM {watchdog} WHERE ". time() ." - timestamp > ". variable_get("watchdog_clear", 604800));
Dries's avatar
 
Dries committed
77
}
Dries's avatar
 
Dries committed
78

Dries's avatar
 
Dries committed
79
function watchdog_overview($type) {
Dries's avatar
 
Dries committed
80 81 82 83
  foreach (_watchdog_get_message_types() as $key) {
    $query[$key] = "WHERE type = '". check_query($key) ."'";
  }
  $query['actions'] = "WHERE link != ''";
Dries's avatar
 
Dries committed
84

Dries's avatar
 
Dries committed
85 86
  $header = array(
    array("data" => t("date"), "field" => "w.timestamp", "sort" => "desc"),
Dries's avatar
 
Dries committed
87
    array("data" => t("message"), "field" => "w.message"),
Dries's avatar
 
Dries committed
88 89 90
    array("data" => t("user"), "field" => "u.name"),
    array("data" => t("operations"), "colspan" => "2")
  );
Dries's avatar
 
Dries committed
91
  $sql = "SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid ". ($type ? $query[$type] : "");
Dries's avatar
 
Dries committed
92 93
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);
Dries's avatar
 
Dries committed
94

Dries's avatar
 
Dries committed
95
  while ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
96 97
    $rows[] = array(
      array("data" => format_date($watchdog->timestamp, "small"), "class" => "watchdog-$watchdog->type"),
98
      array("data" => truncate_utf8(strip_tags($watchdog->message), 64), "class" => "watchdog-$watchdog->type"),
Dries's avatar
 
Dries committed
99 100
      array("data" => format_name($watchdog), "class" => "watchdog-$watchdog->type"),
      array("data" => $watchdog->link, "class" => "watchdog-$watchdog->type"),
Dries's avatar
 
Dries committed
101
      array("data" => l(t("view details"), "admin/logs/view/$watchdog->wid"), "class" => "watchdog-$watchdog->type")
Dries's avatar
 
Dries committed
102
    );
Dries's avatar
 
Dries committed
103
  }
Dries's avatar
 
Dries committed
104

Dries's avatar
 
Dries committed
105
  if (!$rows) {
Dries's avatar
 
Dries committed
106
    $rows[] = array(array("data" => t("No log messages available."), "colspan" => "5"));
Dries's avatar
 
Dries committed
107
  }
Dries's avatar
 
Dries committed
108

Dries's avatar
Dries committed
109
  $pager = theme("pager", NULL, 50, 0, tablesort_pager());
Dries's avatar
 
Dries committed
110 111 112
  if (!empty($pager)) {
    $rows[] = array(array("data" => $pager, "colspan" => "5"));
  }
Dries's avatar
 
Dries committed
113
  return theme("table", $header, $rows);
Dries's avatar
 
Dries committed
114 115 116
}

function watchdog_view($id) {
Dries's avatar
 
Dries committed
117

Dries's avatar
 
Dries committed
118
  $result = db_query("SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = %d", $id);
Dries's avatar
 
Dries committed
119 120

  if ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
121
    $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
Dries's avatar
 
Dries committed
122 123 124 125 126 127
    $output .= " <tr><th>". t("Type") ."</th><td>$watchdog->type</td></tr>";
    $output .= " <tr><th>". t("Date") ."</th><td>". format_date($watchdog->timestamp, "large") ."</td></tr>";
    $output .= " <tr><th>". t("User") ."</th><td>". format_name($watchdog) ."</td></tr>";
    $output .= " <tr><th>". t("Location") ."</th><td>$watchdog->location</td></tr>";
    $output .= " <tr><th>". t("Message") ."</th><td>$watchdog->message</td></tr>";
    $output .= " <tr><th>". t("Hostname") ."</th><td>$watchdog->hostname</td></tr>";
Dries's avatar
 
Dries committed
128
    $output .= "</table>";
Dries's avatar
 
Dries committed
129

Dries's avatar
 
Dries committed
130 131
    return $output;
  }
Dries's avatar
 
Dries committed
132 133
}

Dries's avatar
 
Dries committed
134
function watchdog_admin() {
Dries's avatar
 
Dries committed
135

Dries's avatar
 
Dries committed
136 137 138 139 140 141 142 143 144
  switch (arg(2)) {
    case "help":
      $output = watchdog_help();
      break;
    case "view":
      $output = watchdog_view(arg(3));
      break;
    default:
      $output = watchdog_overview(arg(2));
Dries's avatar
 
Dries committed
145
  }
Dries's avatar
 
Dries committed
146
  print theme("page", $output);
Dries's avatar
 
Dries committed
147
}
Dries's avatar
 
Dries committed
148

Dries's avatar
 
Dries committed
149 150 151 152 153 154 155 156 157 158 159
function _watchdog_get_message_types() {
  $types = array();

  $result = db_query("SELECT DISTINCT(type) FROM {watchdog}");
  while ($object = db_fetch_object($result)) {
    $types[] = $object->type;
  }

  return $types;
}

Dries's avatar
 
Dries committed
160
?>