You are here

watchdog_filtering.module in Watchdog Filtering 7

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

Watchdog Filtering module.

File

watchdog_filtering.module
View source
<?php

/**
 * @file
 * Watchdog Filtering module.
 */

/**
 * Return this value from hook_watchdog_filtering() to exclude message.
 */
define('WATCHDOG_FILTERING_EXCLUDE', 'exclude');

/**
 * Return this value from hook_watchdog_filtering() to include message.
 */
define('WATCHDOG_FILTERING_INCLUDE', 'include');

/**
 * Return this value from hook_watchdog_filtering() to not filter on message.
 */
define('WATCHDOG_FILTERING_IGNORE', 'ignore');

/**
 * Implements hook_menu().
 */
function watchdog_filtering_menu() {
  $items = array();
  $items['admin/config/development/logging/general'] = array(
    'title' => 'Logging and errors',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/config/development/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) {
  if ($hook == 'watchdog') {

    // Make sure only Watchdog Filtering is invoked during watchdogging.
    foreach ($implementations as $module => $group) {

      // If this hook implementation is stored in a lazy-loaded file, so include
      // that file first.
      if ($group) {
        module_load_include('inc', $module, "{$module}.{$group}");
      }
      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[$module]);
      }
    }
    unset($implementations['watchdog_filtering']);

    // Drush injects it's very own watchdog hook as system_watchdog)(). Add that
    // hook to ensure we don't have to update the variable between drush /
    // webserver calls.
    // watchdog_filtering_watchdog() will check if the function exists before
    // calling it.
    $implementations['system'] = FALSE;

    // Only update if there were actual changes. Optimizes page processing /
    // delivery performance.
    if (variable_get('watchdog_filtering_implementations', array()) != $implementations) {

      // Set this to the loaded settings to avoid unnecessary re-saves.
      $GLOBALS['conf']['watchdog_filtering_implementations'] = $implementations;

      // Register the variable storage for the shutdown process.
      drupal_register_shutdown_function('variable_set', 'watchdog_filtering_implementations', $implementations);
    }
    $implementations = array(
      'watchdog_filtering' => FALSE,
    );
  }
}

/**
 * Get a list of filtering levels.
 *
 * @return array
 *   An array of filtering levels
 */
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
 *   A log entry to check.
 *
 * @return boolean
 *   Whether the log entry should be filtered or not.
 */
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 = variable_get('watchdog_filtering_implementations', array());
  $return = array();
  foreach ($implementations as $module => $group) {
    $function = $module . '_watchdog';
    if (function_exists($function)) {
      call_user_func($function, $log_entry);
    }
    else {

      // Temporary remove this implementation so that we only check once per
      // request.
      unset($GLOBALS['conf']['watchdog_filtering_implementations'][$module]);
    }
  }
  return $return;
}

Functions

Constants

Namesort descending Description
WATCHDOG_FILTERING_EXCLUDE Return this value from hook_watchdog_filtering() to exclude message.
WATCHDOG_FILTERING_IGNORE Return this value from hook_watchdog_filtering() to not filter on message.
WATCHDOG_FILTERING_INCLUDE Return this value from hook_watchdog_filtering() to include message.