Commit 38d22252 authored by Dries's avatar Dries

- Patch #67893 by kbahey, dww, alienbrain, asimmonds et al: made it possible...

- Patch #67893 by kbahey, dww, alienbrain, asimmonds et al: made it possible to filter log messages in the database log.
parent dad52753
......@@ -6,7 +6,8 @@ Drupal 6.0, xxxx-xx-xx (development version)
* New hook_watchdog that can be implemented by any module to route log messages to various destinations.
* Expands the severity levels from 3 (Error, Warning, Notice) to the 8 levels defined in RFC 3164.
* The watchdog module is now called dblog, and is optional, but enabled by default in the default install profile.
* New optional syslog.module now in core.
* Extended the database log module so log messages can be filtered.
* Added syslog module: useful for monitoring large Drupal installations.
- Added theme registry: modules can directly provide .tpl.php files for their themes without having to create theme_ functions.
- Added versioning support to node terms.
- Made it easier to theme the forum overview page.
......
......@@ -37,6 +37,8 @@
/**
*
* Severity levels, as defined in RFC 3164 http://www.faqs.org/rfcs/rfc3164.html
* @see watchdog
* @see watchdog_severity_levels
*/
define('WATCHDOG_EMERG', 0); // Emergency: system is unusable
define('WATCHDOG_ALERT', 1); // Alert: action must be taken immediately
......@@ -657,6 +659,8 @@ function request_uri() {
* The severity of the message, as per RFC 3164
* @param $link
* A link to associate with the message.
*
* @see watchdog_severity_levels
*/
function watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) {
global $user, $base_root;
......
......@@ -2616,3 +2616,22 @@ function drupal_parse_info_file($filename) {
return $info;
}
/**
* @return
* Array of the possible severity levels for log messages.
*
* @see watchdog
*/
function watchdog_severity_levels() {
return array(
WATCHDOG_EMERG => t('emergency'),
WATCHDOG_ALERT => t('alert'),
WATCHDOG_CRITICAL => t('critical'),
WATCHDOG_ERROR => t('error'),
WATCHDOG_WARNING => t('warning'),
WATCHDOG_NOTICE => t('notice'),
WATCHDOG_INFO => t('info'),
WATCHDOG_DEBUG => t('debug'),
);
}
/* $Id$ */
#dblog-filter-form .form-item {
float: left;
padding-right: .8em;
margin: 0.1em;
}
tr.dblog-user {
background: #ffd;
}
......
......@@ -32,7 +32,7 @@ function dblog_help($section) {
*/
function dblog_theme() {
return array(
'dblog_form_overview' => array(
'dblog_filters' => array(
'arguments' => array('form' => NULL),
),
);
......@@ -122,38 +122,24 @@ function dblog_user($op, &$edit, &$user) {
}
}
function dblog_form_overview() {
$names['all'] = t('all messages');
foreach (_dblog_get_message_types() as $type) {
$names[$type] = t('!type messages', array('!type' => t($type)));
}
if (empty($_SESSION['dblog_overview_filter'])) {
$_SESSION['dblog_overview_filter'] = 'all';
}
$form['filter'] = array(
'#type' => 'select',
'#title' => t('Filter by message type'),
'#options' => $names,
'#default_value' => $_SESSION['dblog_overview_filter']
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Filter'));
$form['#redirect'] = FALSE;
return $form;
}
/**
* Menu callback; displays a listing of log messages.
*/
function dblog_overview() {
$filter = dblog_build_filter_query();
$rows = array();
$icons = array(WATCHDOG_NOTICE => '',
WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
WATCHDOG_ERROR => theme('image', 'misc/watchdog-error.png', t('error'), t('error')));
$classes = array(WATCHDOG_NOTICE => 'dblog-notice', WATCHDOG_WARNING => 'dblog-warning', WATCHDOG_ERROR => 'dblog-error');
$icons = array(
WATCHDOG_NOTICE => '',
WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
WATCHDOG_ERROR => theme('image', 'misc/watchdog-error.png', t('error'), t('error')),
);
$classes = array(
WATCHDOG_NOTICE => 'dblog-notice',
WATCHDOG_WARNING => 'dblog-warning',
WATCHDOG_ERROR => 'dblog-error',
);
$output = drupal_get_form('dblog_form_overview');
$output = drupal_get_form('dblog_filter_form');
$header = array(
' ',
......@@ -161,14 +147,13 @@ function dblog_overview() {
array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
t('Message'),
array('data' => t('User'), 'field' => 'u.name'),
array('data' => t('Operations'))
array('data' => t('Operations')),
);
$sql = "SELECT w.wid, w.uid, w.severity, w.type, w.timestamp, w.message, w.variables, w.link, u.name FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid";
$tablesort = tablesort_sql($header);
$type = $_SESSION['dblog_overview_filter'];
if ($type != 'all') {
$result = pager_query($sql ." WHERE w.type = '%s'". $tablesort, 50, 0, NULL, $type);
if (!empty($filter['where'])) {
$result = pager_query($sql ." WHERE ". $filter['where'] . $tablesort, 50, 0, NULL, $filter['args']);
}
else {
$result = pager_query($sql . $tablesort, 50);
......@@ -228,19 +213,11 @@ function dblog_top($type) {
return $output;
}
function theme_dblog_form_overview($form) {
return '<div class="container-inline">'. drupal_render($form) .'</div>';
}
function dblog_form_overview_submit($form_id, $form_values) {
$_SESSION['dblog_overview_filter'] = $form_values['filter'];
}
/**
* Menu callback; displays details about a log message.
*/
function dblog_event($id) {
$severity = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
$severity = watchdog_severity_levels();
$output = '';
$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);
if ($dblog = db_fetch_object($result)) {
......@@ -337,3 +314,141 @@ function _dblog_format_message($dblog) {
return t($dblog->message, unserialize($dblog->variables));
}
}
/**
* Return form for dblog administration filters.
*/
function dblog_filter_form() {
$session = &$_SESSION['dblog_overview_filter'];
$session = is_array($session) ? $session : array();
$filters = dblog_filters();
$form['filters'] = array(
'#type' => 'fieldset',
'#title' => t('Filter log messages'),
'#theme' => 'dblog_filters',
'#collapsible' => TRUE,
'#collapsed' => empty($session),
);
foreach ($filters as $key => $filter) {
$form['filters']['status'][$key] = array(
'#title' => $filter['title'],
'#type' => 'select',
'#multiple' => TRUE,
'#size' => 8,
'#options' => $filter['options'],
);
if (!empty($session[$key])) {
$form['filters']['status'][$key]['#default_value'] = $session[$key];
}
}
$form['filters']['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Filter'),
);
if (!empty($session)) {
$form['filters']['buttons']['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset')
);
}
return $form;
}
/**
* 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;
}
function dblog_filter_form_validate($form_id, $form_values) {
if ($form_values['op'] == t('Filter') && empty($form_values['type']) && empty($form_values['severity'])) {
form_set_error('type', t('You must select something to filter by.'));
}
}
/**
* Process result from dblog administration filter form.
*/
function dblog_filter_form_submit($form_id, $form_values) {
$op = $form_values['op'];
$filters = dblog_filters();
switch ($op) {
case t('Filter'):
foreach ($filters as $name => $filter) {
if (isset($form_values[$name])) {
$_SESSION['dblog_overview_filter'][$name] = $form_values[$name];
}
}
break;
case t('Reset'):
$_SESSION['dblog_overview_filter'] = array();
break;
}
return 'admin/logs/dblog';
}
/**
* List dblog administration filters that can be applied.
*/
function dblog_filters() {
$filters = array();
foreach(_dblog_get_message_types() as $type) {
$types[$type] = $type;
}
if (!empty($types)) {
$filters['type'] = array(
'title' => t('Type'),
'where' => "w.type = '%s'",
'options' => $types,
);
}
$filters['severity'] = array(
'title' => t('Severity'),
'where' => 'w.severity = %d',
'options' => watchdog_severity_levels(),
);
return $filters;
}
/**
* Build query for dblog administration filters based on session.
*/
function dblog_build_filter_query() {
if (empty($_SESSION['dblog_overview_filter'])) {
return;
}
$filters = dblog_filters();
// Build query
$where = $args = array();
foreach ($_SESSION['dblog_overview_filter'] as $key => $filter) {
$filter_where = array();
foreach ($filter as $value) {
$filter_where[] = $filters[$key]['where'];
$args[] = $value;
}
if (!empty($filter_where)) {
$where[] = '('. implode(' OR ', $filter_where) .')';
}
}
$where = !empty($where) ? implode(' AND ', $where) : '';
return array(
'where' => $where,
'args' => $args,
);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment