watchdog.module 6.65 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 11
      $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>
      <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/watchdog"), "%log-entry" => url("admin/system/modules/watchdog")));
Dries's avatar
 
Dries committed
12
      break;
Dries's avatar
 
Dries committed
13 14
   case 'admin/watchdog':
     $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.");
Dries's avatar
 
Dries committed
15
     break;
Dries's avatar
 
Dries committed
16 17
  case 'admin/watchdog/user':
     $output = t("Watchdog events that have to do with users.");
Dries's avatar
 
Dries committed
18
     break;
Dries's avatar
 
Dries committed
19 20
  case 'admin/watchdog/regular':
    $output = t("Watchdog events that are \"normal\" and have no other classification.");
Dries's avatar
 
Dries committed
21
    break;
Dries's avatar
 
Dries committed
22 23
  case 'admin/watchdog/httpd':
    $output = t("Watchdog events that are from the web server.");
Dries's avatar
 
Dries committed
24
    break;
Dries's avatar
 
Dries committed
25 26
  case 'admin/watchdog/special':
    $output = t("Watchdog events about adding, changing, and moderating nodes and comments.");
Dries's avatar
 
Dries committed
27
    break;
Dries's avatar
 
Dries committed
28 29 30
  case 'admin/watchdog/search':
    $output = t("Watchdog events showing what users searched for.");
    break;
Dries's avatar
 
Dries committed
31
  case 'admin/watchdog/error':
Dries's avatar
Dries committed
32
    $output = t("Watchdog events about PHP and database errors.");
Dries's avatar
 
Dries committed
33
    break;
Dries's avatar
 
Dries committed
34
  case 'admin/watchdog/warning':
Dries's avatar
 
Dries committed
35
    $output = t("Watchdog warning events. These events don't stop Drupal from running, but are things you should know.");
Dries's avatar
 
Dries committed
36 37 38 39 40
    break;
  case 'admin/system/modules#description':
    $output = t("Logs and records system events.");
    break;
  case 'admin/system/modules/watchdog':
Dries's avatar
 
Dries committed
41
    $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/watchdog")));
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
function watchdog_link($type) {
Dries's avatar
 
Dries committed
53 54
  if ($type == "system") {
    if (user_access("administer watchdog")) {
55 56
      menu("admin/watchdog", t("logs"), "watchdog_admin", 7);
      menu("admin/watchdog/view", t("view details"), "watchdog_admin", 0, MENU_HIDE, MENU_LOCKED);
Dries's avatar
 
Dries committed
57

58 59
      foreach (_watchdog_get_message_types() as $type) {
        menu("admin/watchdog/$type", t($type), "watchdog_admin");
Dries's avatar
 
Dries committed
60
      }
Dries's avatar
 
Dries committed
61
    }
Dries's avatar
 
Dries committed
62 63 64
  }
}

65
function watchdog_settings() {
Dries's avatar
 
Dries committed
66 67
  $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
68
  $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
69 70 71
  return $output;
}

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

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

Dries's avatar
 
Dries committed
82 83
  $header = array(
    array("data" => t("date"), "field" => "w.timestamp", "sort" => "desc"),
Dries's avatar
 
Dries committed
84
    array("data" => t("message"), "field" => "w.message"),
Dries's avatar
 
Dries committed
85 86 87
    array("data" => t("user"), "field" => "u.name"),
    array("data" => t("operations"), "colspan" => "2")
  );
Dries's avatar
 
Dries committed
88
  $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
89 90
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);
Dries's avatar
 
Dries committed
91

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

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

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

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

Dries's avatar
 
Dries committed
115
  $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
116 117

  if ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
118
    $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
Dries's avatar
 
Dries committed
119 120 121 122 123 124
    $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
125
    $output .= "</table>";
Dries's avatar
 
Dries committed
126

Dries's avatar
 
Dries committed
127 128
    return $output;
  }
Dries's avatar
 
Dries committed
129 130
}

Dries's avatar
 
Dries committed
131
function watchdog_admin() {
Dries's avatar
 
Dries committed
132

Dries's avatar
 
Dries committed
133 134 135 136 137 138 139 140 141
  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
142
  }
Dries's avatar
 
Dries committed
143
  print theme("page", $output);
Dries's avatar
 
Dries committed
144
}
Dries's avatar
 
Dries committed
145

Dries's avatar
 
Dries committed
146 147 148 149 150 151 152 153 154 155 156
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
157
?>