web/drupal/modules/dblog/dblog.module
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: dblog.module,v 1.21.2.2 2008/04/09 21:11:46 goba Exp $
       
     3 
       
     4 /**
       
     5  * @file
       
     6  * System monitoring and logging for administrators.
       
     7  *
       
     8  * The dblog module monitors your site and keeps a list of
       
     9  * recorded events containing usage and performance data, errors,
       
    10  * warnings, and similar operational information.
       
    11  *
       
    12  * @see watchdog()
       
    13  */
       
    14 
       
    15 /**
       
    16  * Implementation of hook_help().
       
    17  */
       
    18 function dblog_help($path, $arg) {
       
    19   switch ($path) {
       
    20     case 'admin/help#dblog':
       
    21       $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>';
       
    22       $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>';
       
    23       $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>';
       
    27   }
       
    28 }
       
    29 
       
    30 /**
       
    31  * Implementation of hook_theme()
       
    32  */
       
    33 function dblog_theme() {
       
    34   return array(
       
    35     'dblog_filters' => array(
       
    36       'arguments' => array('form' => NULL),
       
    37     ),
       
    38   );
       
    39 }
       
    40 
       
    41 /**
       
    42  * Implementation of hook_menu().
       
    43  */
       
    44 function dblog_menu() {
       
    45   $items['admin/settings/logging/dblog'] = array(
       
    46     'title' => 'Database logging',
       
    47     '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     'page callback' => 'drupal_get_form',
       
    49     'page arguments' => array('dblog_admin_settings'),
       
    50     'access arguments' => array('administer site configuration'),
       
    51     'file' => 'dblog.admin.inc',
       
    52   );
       
    53 
       
    54   $items['admin/reports/dblog'] = array(
       
    55     'title' => 'Recent log entries',
       
    56     'description' => 'View events that have recently been logged.',
       
    57     'page callback' => 'dblog_overview',
       
    58     'access arguments' => array('access site reports'),
       
    59     'weight' => -1,
       
    60     'file' => 'dblog.admin.inc',
       
    61   );
       
    62   $items['admin/reports/page-not-found'] = array(
       
    63     'title' => "Top 'page not found' errors",
       
    64     'description' => "View 'page not found' errors (404s).",
       
    65     'page callback' => 'dblog_top',
       
    66     'page arguments' => array('page not found'),
       
    67     'access arguments' => array('access site reports'),
       
    68     'file' => 'dblog.admin.inc',
       
    69   );
       
    70   $items['admin/reports/access-denied'] = array(
       
    71     'title' => "Top 'access denied' errors",
       
    72     'description' => "View 'access denied' errors (403s).",
       
    73     'page callback' => 'dblog_top',
       
    74     'page arguments' => array('access denied'),
       
    75     'access arguments' => array('access site reports'),
       
    76     'file' => 'dblog.admin.inc',
       
    77   );
       
    78   $items['admin/reports/event/%'] = array(
       
    79     'title' => 'Details',
       
    80     'page callback' => 'dblog_event',
       
    81     'page arguments' => array(3),
       
    82     'access arguments' => array('access site reports'),
       
    83     'type' => MENU_CALLBACK,
       
    84     'file' => 'dblog.admin.inc',
       
    85   );
       
    86   return $items;
       
    87 }
       
    88 
       
    89 function dblog_init() {
       
    90   if (arg(0) == 'admin' && arg(1) == 'reports') {
       
    91     // Add the CSS for this module
       
    92     drupal_add_css(drupal_get_path('module', 'dblog') .'/dblog.css', 'module', 'all', FALSE);
       
    93   }
       
    94 }
       
    95 
       
    96 
       
    97 
       
    98 /**
       
    99  * Implementation of hook_cron().
       
   100  *
       
   101  * Remove expired log messages and flood control events.
       
   102  */
       
   103 function dblog_cron() {
       
   104   // Cleanup the watchdog table
       
   105   $max = db_result(db_query('SELECT MAX(wid) FROM {watchdog}'));
       
   106   db_query('DELETE FROM {watchdog} WHERE wid <= %d', $max - variable_get('dblog_row_limit', 1000));
       
   107 }
       
   108 
       
   109 /**
       
   110  * Implementation of hook_user().
       
   111  */
       
   112 function dblog_user($op, &$edit, &$user) {
       
   113   if ($op == 'delete') {
       
   114     db_query('UPDATE {watchdog} SET uid = 0 WHERE uid = %d', $user->uid);
       
   115   }
       
   116 }
       
   117 
       
   118 function _dblog_get_message_types() {
       
   119   $types = array();
       
   120 
       
   121   $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
       
   122   while ($object = db_fetch_object($result)) {
       
   123     $types[] = $object->type;
       
   124   }
       
   125 
       
   126   return $types;
       
   127 }
       
   128 
       
   129 function dblog_watchdog($log = array()) {
       
   130   $current_db = db_set_active();
       
   131   db_query("INSERT INTO {watchdog}
       
   132     (uid, type, message, variables, severity, link, location, referer, hostname, timestamp)
       
   133     VALUES
       
   134     (%d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', %d)",
       
   135     $log['user']->uid,
       
   136     $log['type'],
       
   137     $log['message'],
       
   138     serialize($log['variables']),
       
   139     $log['severity'],
       
   140     $log['link'],
       
   141     $log['request_uri'],
       
   142     $log['referer'],
       
   143     $log['ip'],
       
   144     $log['timestamp']);
       
   145 
       
   146   if ($current_db) {
       
   147     db_set_active($current_db);
       
   148   }
       
   149 }
       
   150 
       
   151 /**
       
   152  * Theme dblog administration filter selector.
       
   153  *
       
   154  * @ingroup themeable
       
   155  */
       
   156 function theme_dblog_filters($form) {
       
   157   $output = '';
       
   158   foreach (element_children($form['status']) as $key) {
       
   159     $output .= drupal_render($form['status'][$key]);
       
   160   }
       
   161   $output .= '<div id="dblog-admin-buttons">'. drupal_render($form['buttons']) .'</div>';
       
   162   return $output;
       
   163 }
       
   164