2001-03-10 11:07:52 +00:00
<?php
2000-12-14 14:13:37 +00:00
2004-07-02 07:11:35 +00:00
/**
* @file
* System monitoring and logging for administrators.
*
2012-08-29 18:15:05 +00:00
* The Database Logging module monitors your site and keeps a list of recorded
* events containing usage and performance data, errors, warnings, and similar
* operational information.
2004-07-02 07:11:35 +00:00
*
2008-01-08 10:35:43 +00:00
* @see watchdog()
2004-07-02 07:11:35 +00:00
*/
2012-10-26 16:04:02 +00:00
use Drupal\Core\Database\Database;
2004-05-24 18:37:50 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help().
2004-05-24 18:37:50 +00:00
*/
2007-06-30 19:46:58 +00:00
function dblog_help($path, $arg) {
switch ($path) {
2007-04-10 10:10:27 +00:00
case 'admin/help#dblog':
2009-12-06 18:11:41 +00:00
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2012-03-21 05:51:30 +00:00
$output .= '<p>' . t('The Database Logging module logs system events in the Drupal database. For more information, see the online handbook entry for the <a href="@dblog">Database Logging module</a>.', array('@dblog' => 'http://drupal.org/documentation/modules/dblog')) . '</p>';
2009-12-06 18:11:41 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Monitoring your site') . '</dt>';
2012-02-29 21:15:08 +00:00
$output .= '<dd>' . t('The Database Logging module allows you to view an event log on the <a href="@dblog">Recent log messages</a> page. The log is a chronological list of recorded events containing usage data, performance data, errors, warnings and operational information. Administrators should check the log on a regular basis to ensure their site is working properly.', array('@dblog' => url('admin/reports/dblog'))) . '</dd>';
2009-12-06 18:11:41 +00:00
$output .= '<dt>' . t('Debugging site problems') . '</dt>';
2010-09-01 03:03:05 +00:00
$output .= '<dd>' . t('In case of errors or problems with the site, the <a href="@dblog">Recent log messages</a> page can be useful for debugging, since it shows the sequence of events. The log messages include usage information, warnings, and errors.', array('@dblog' => url('admin/reports/dblog'))) . '</dd>';
2009-12-06 18:11:41 +00:00
$output .= '</dl>';
2010-01-30 07:59:26 +00:00
return $output;
2007-10-20 21:57:50 +00:00
case 'admin/reports/dblog':
2012-02-29 21:15:08 +00:00
return '<p>' . t('The Database Logging module monitors your website, capturing system events in a log (shown here) to be reviewed by an authorized individual at a later time. This log is a list of recorded events containing usage data, performance data, errors, warnings and operational information. It is vital to check the Recent log messages report on a regular basis, as it is often the only way to tell what is going on.') . '</p>';
2003-08-19 19:59:33 +00:00
}
2002-06-01 21:57:29 +00:00
}
2004-04-21 13:56:38 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_menu().
2004-04-21 13:56:38 +00:00
*/
2007-04-10 10:10:27 +00:00
function dblog_menu() {
2007-10-20 21:57:50 +00:00
$items['admin/reports/dblog'] = array(
2010-09-01 03:03:05 +00:00
'title' => 'Recent log messages',
2007-04-30 17:03:29 +00:00
'description' => 'View events that have recently been logged.',
2013-05-19 20:05:02 +00:00
'route_name' => 'dblog_overview',
2007-01-24 14:48:36 +00:00
'weight' => -1,
);
2007-10-20 21:57:50 +00:00
$items['admin/reports/page-not-found'] = array(
2007-04-30 17:03:29 +00:00
'title' => "Top 'page not found' errors",
'description' => "View 'page not found' errors (404s).",
2007-04-10 10:10:27 +00:00
'page callback' => 'dblog_top',
2007-01-24 14:48:36 +00:00
'page arguments' => array('page not found'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('access site reports'),
2009-08-24 00:14:23 +00:00
'file' => 'dblog.admin.inc',
2007-01-24 14:48:36 +00:00
);
2007-10-20 21:57:50 +00:00
$items['admin/reports/access-denied'] = array(
2007-12-31 15:51:30 +00:00
'title' => "Top 'access denied' errors",
2007-04-30 17:03:29 +00:00
'description' => "View 'access denied' errors (403s).",
2007-04-10 10:10:27 +00:00
'page callback' => 'dblog_top',
2007-01-24 14:48:36 +00:00
'page arguments' => array('access denied'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('access site reports'),
2009-08-24 00:14:23 +00:00
'file' => 'dblog.admin.inc',
2007-01-24 14:48:36 +00:00
);
2007-10-20 21:57:50 +00:00
$items['admin/reports/event/%'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Details',
2007-04-10 10:10:27 +00:00
'page callback' => 'dblog_event',
2007-01-24 14:48:36 +00:00
'page arguments' => array(3),
2008-04-23 20:01:56 +00:00
'access arguments' => array('access site reports'),
2009-08-24 00:14:23 +00:00
'file' => 'dblog.admin.inc',
2007-01-24 14:48:36 +00:00
);
2010-06-29 00:39:16 +00:00
if (module_exists('search')) {
$items['admin/reports/search'] = array(
'title' => 'Top search phrases',
'description' => 'View most popular search phrases.',
'page callback' => 'dblog_top',
'page arguments' => array('search'),
'access arguments' => array('access site reports'),
'file' => 'dblog.admin.inc',
);
}
2007-01-24 14:48:36 +00:00
return $items;
}
2004-07-03 14:10:09 +00:00
2010-01-27 11:28:49 +00:00
/**
2012-12-13 12:23:00 +00:00
* Implements hook_page_build().
2010-01-27 11:28:49 +00:00
*/
2012-12-13 12:23:00 +00:00
function dblog_page_build(&$page) {
2007-11-27 16:24:34 +00:00
if (arg(0) == 'admin' && arg(1) == 'reports') {
2013-06-07 10:48:55 +00:00
$page['#attached']['css'][] = drupal_get_path('module', 'dblog') . '/css/dblog.module.css';
2006-08-14 07:14:50 +00:00
}
2001-05-19 13:41:52 +00:00
}
2004-05-24 18:37:50 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_cron().
2004-05-24 18:37:50 +00:00
*
2012-08-29 18:15:05 +00:00
* Controls the size of the log table, paring it to 'dblog_row_limit' messages.
2004-05-24 18:37:50 +00:00
*/
2007-04-10 10:10:27 +00:00
function dblog_cron() {
2010-10-03 02:14:23 +00:00
// Cleanup the watchdog table.
2012-07-28 16:35:17 +00:00
$row_limit = config('dblog.settings')->get('row_limit');
2010-10-03 02:14:23 +00:00
// For row limit n, get the wid of the nth row in descending wid order.
// Counting the most recent n rows avoids issues with wid number sequences,
// e.g. auto_increment value > 1 or rows deleted directly from the table.
if ($row_limit > 0) {
$min_row = db_select('watchdog', 'w')
->fields('w', array('wid'))
->orderBy('wid', 'DESC')
->range($row_limit - 1, 1)
->execute()->fetchField();
// Delete all table entries older than the nth row, if nth row was found.
if ($min_row) {
db_delete('watchdog')
->condition('wid', $min_row, '<')
->execute();
}
2009-08-29 03:36:27 +00:00
}
2000-12-16 08:39:01 +00:00
}
2000-12-14 14:13:37 +00:00
2012-08-29 18:15:05 +00:00
/**
* Gathers a list of uniquely defined database log message types.
*
* @return array
* List of uniquely defined database log message types.
*/
2007-04-10 10:10:27 +00:00
function _dblog_get_message_types() {
2004-01-02 16:28:45 +00:00
$types = array();
2005-01-09 09:22:40 +00:00
$result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
2009-03-13 14:28:18 +00:00
foreach ($result as $object) {
2004-01-02 16:28:45 +00:00
$types[] = $object->type;
}
return $types;
}
2007-04-10 10:10:27 +00:00
2008-12-28 20:41:19 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_watchdog().
2008-12-28 20:41:19 +00:00
*
2012-08-29 18:15:05 +00:00
* Note: Some values may be truncated to meet database column size restrictions.
2008-12-28 20:41:19 +00:00
*/
function dblog_watchdog(array $log_entry) {
2013-03-27 05:32:28 +00:00
// Remove any backtraces since they may contain an unserializable variable.
unset($log_entry['variables']['backtrace']);
2009-01-25 12:19:32 +00:00
Database::getConnection('default', 'default')->insert('watchdog')
2008-10-30 09:16:01 +00:00
->fields(array(
2012-04-02 03:12:33 +00:00
'uid' => $log_entry['uid'],
2008-12-28 20:41:19 +00:00
'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'],
2008-10-30 09:16:01 +00:00
))
->execute();
2007-04-10 10:10:27 +00:00
}
2007-04-24 13:53:15 +00:00
2009-08-24 00:14:23 +00:00
/**
2012-08-29 18:15:05 +00:00
* Implements hook_form_FORM_ID_alter() for system_logging_settings().
2009-08-24 00:14:23 +00:00
*/
function dblog_form_system_logging_settings_alter(&$form, $form_state) {
$form['dblog_row_limit'] = array(
'#type' => 'select',
2010-09-01 03:03:05 +00:00
'#title' => t('Database log messages to keep'),
2012-07-28 16:35:17 +00:00
'#default_value' => config('dblog.settings')->get('row_limit'),
2009-08-29 03:36:27 +00:00
'#options' => array(0 => t('All')) + drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
2010-09-01 03:03:05 +00:00
'#description' => t('The maximum number of messages to keep in the database log. Requires a <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status')))
2009-08-24 00:14:23 +00:00
);
2012-07-28 16:35:17 +00:00
$form['#submit'][] = 'dblog_logging_settings_submit';
}
/**
* Form submission handler for system_logging_settings().
*
* @see dblog_form_system_logging_settings_alter()
*/
function dblog_logging_settings_submit($form, &$form_state) {
config('dblog.settings')->set('row_limit', $form_state['values']['dblog_row_limit'])->save();
2009-08-24 00:14:23 +00:00
}