You are here

watchdog_filtering.module in Watchdog Filtering 6

Same filename and directory in other branches
  1. 7 watchdog_filtering.module

File

watchdog_filtering.module
View source
<?php

/**
 * @file
 */

/**
 * Modules should return this value from hook_watchdog_filtering() to exclude watchdog message.
 */
define('WATCHDOG_FILTERING_EXCLUDE', 'exclude');

/**
 * Modules should return this value from hook_watchdog_filtering() to include watchdog message.
 */
define('WATCHDOG_FILTERING_INCLUDE', 'include');

/**
 * Modules should return this value from hook_watchdog_filtering() to perform no filtering on watchdog message.
 */
define('WATCHDOG_FILTERING_IGNORE', 'ignore');

/**
 * Implements hook_menu().
 */
function watchdog_filtering_menu() {
  $items = array();
  $items['admin/settings/logging/general'] = array(
    'title' => 'Logging and errors',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/settings/logging/filtering'] = array(
    'title' => 'Filtering',
    'description' => 'Watchdog Filtering settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'watchdog_filtering_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'watchdog_filtering.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_module_implements_alter().
 */
function watchdog_filtering_module_implements_alter(&$implementations, $hook) {
  static $has_run = FALSE;
  if ($has_run) {
    return;
  }
  if ($hook == 'watchdog') {
    $has_run = TRUE;

    // Make sure only Watchdog Filtering is invoked during watchdogging.
    foreach ($implementations[$hook] as $idx => $module) {
      if ($module == 'watchdog_filtering') {
        unset($implementations[$hook][$idx]);
        continue;
      }

      // If this hook implementation is stored in a lazy-loaded file, so include
      // that file first.
      module_load_include('inc', $module);
      if (!function_exists($module . '_' . $hook)) {

        // Clear out the stale implementation from the cache and force a cache
        // refresh to forget about no longer existing hook implementations.
        unset($implementations[$hook][$idx]);
      }
    }

    // variable_set('watchdog_filtering_implementations', $implementations);
    $watchdog_filtering_implementations =& drupal_static('watchdog_filtering_implementations', array());
    $watchdog_filtering_implementations = array_values($implementations[$hook]);
    $implementations[$hook] = array(
      'watchdog_filtering',
    );
  }
}
function _watchdog_filtering_get_levels() {
  global $conf;
  $levels = array();
  foreach ($conf as $key => $value) {
    if (preg_match('/^watchdog_filtering_severity_(.*)$/', $key, $matches)) {
      $levels[$matches[1]] = $value;
    }
  }
  return $levels;
}

/**
 * Check if a log entry may be watchdogged.
 *
 * @param array $log_entry
 *
 * @return boolean
 */
function watchdog_filtering_invoke_filtering($log_entry) {
  $unique =& drupal_static('watchdog_filtering_unique', array());
  if (variable_get('watchdog_filtering_deduplicate', FALSE)) {
    $key = md5(serialize($log_entry));
    if (isset($unique[$key])) {
      return WATCHDOG_FILTERING_EXCLUDE;
    }
    $unique[$key] = TRUE;
  }

  // Get severity level configuration for this type
  $type = rawurlencode($log_entry['type']);
  $severity = variable_get('watchdog_filtering_severity_' . $type, NULL);
  if (!isset($severity)) {
    $severity = FALSE;
    variable_set('watchdog_filtering_severity_' . $type, $severity);
  }
  if ($severity === FALSE) {
    $severity = variable_get('watchdog_filtering_default_severity', array_keys(watchdog_severity_levels()));
  }

  // Invoke custom hooks if any.
  $status = module_invoke_all('watchdog_filtering', $log_entry);
  if (in_array(WATCHDOG_FILTERING_EXCLUDE, $status, TRUE)) {
    return WATCHDOG_FILTERING_EXCLUDE;
  }
  elseif (in_array(WATCHDOG_FILTERING_INCLUDE, $status, TRUE)) {
    return WATCHDOG_FILTERING_INCLUDE;
  }

  // Respect configured severity level for this log entry.
  if (in_array($log_entry['severity'], $severity)) {
    return WATCHDOG_FILTERING_INCLUDE;
  }
  else {
    return WATCHDOG_FILTERING_EXCLUDE;
  }
}

/**
 * Implements hook_watchdog().
 *
 * Check watchdog filtering and dispatch to the originally defined hook_watchdog() implementations.
 */
function watchdog_filtering_watchdog(array $log_entry) {
  if (watchdog_filtering_invoke_filtering($log_entry) === WATCHDOG_FILTERING_EXCLUDE) {

    // Don't watchdog this log entry
    return;
  }
  $implementations = drupal_static('watchdog_filtering_implementations', array());
  $return = array();
  foreach ($implementations as $module) {
    $function = $module . '_watchdog';
    if (function_exists($function)) {
      call_user_func($function, $log_entry);
    }
  }
  return $return;
}

Functions

Namesort descending Description
watchdog_filtering_invoke_filtering Check if a log entry may be watchdogged.
watchdog_filtering_menu Implements hook_menu().
watchdog_filtering_module_implements_alter Implements hook_module_implements_alter().
watchdog_filtering_watchdog Implements hook_watchdog().
_watchdog_filtering_get_levels

Constants

Namesort descending Description
WATCHDOG_FILTERING_EXCLUDE Modules should return this value from hook_watchdog_filtering() to exclude watchdog message.
WATCHDOG_FILTERING_IGNORE Modules should return this value from hook_watchdog_filtering() to perform no filtering on watchdog message.
WATCHDOG_FILTERING_INCLUDE Modules should return this value from hook_watchdog_filtering() to include watchdog message.