You are here

function emaillog_watchdog in Logging and alerts 6.2

Same name and namespace in other branches
  1. 6 emaillog/emaillog.module \emaillog_watchdog()
  2. 7.2 emaillog/emaillog.module \emaillog_watchdog()
  3. 7 emaillog/emaillog.module \emaillog_watchdog()

Implementation of hook_watchdog().

File

emaillog/emaillog.module, line 59
Drupal Module: Email Logging and Alerts

Code

function emaillog_watchdog($log) {
  global $language;

  // Send email only if there is an email address.
  // Otherwise the message is ignored by this module.
  if (!($to = variable_get('emaillog_' . $log['severity'], NULL))) {
    return;
  }

  // Assume that current message is not a repetition.
  $message_count = 1;

  // Check if main email repetition restricting options are set.
  // It's enough to check only emaillog_max_similar_emails variable,
  // as setting it requires emaillog_max_similarity_level to be set as well.
  // Saving few unnecessary database queries this way if it's not set.
  $max_similar_emails = variable_get('emaillog_max_similar_emails', NULL);
  if ($max_similar_emails) {
    $max_similarity_level = variable_get('emaillog_max_similarity_level', NULL);

    // Get previously sent message data and compare its content with current one.
    $last_message = variable_get('emaillog_last_message', NULL);
    $max_length = isset($last_message['message']) ? max(strlen($log['message']), strlen($last_message['message'])) : strlen($log['message']);
    $similarity = 0;
    if ($max_length > 0) {
      similar_text($log['type'] . $log['message'], $last_message['message'], $similarity);
      $similarity /= 100;
    }

    // If similarity level is higher than allowed in module configuration,
    // and if maximum number of similar messages to sent was reached,
    // stop execution and return - no email should be sent in such case.
    if ($similarity > $max_similarity_level) {
      if ($last_message['count'] >= $max_similar_emails) {

        // Also make sure that those similar emails are consecutive,
        // ie. were sent during a specific period of time (if defined).
        $max_consecutive_timespan = variable_get('emaillog_max_consecutive_timespan', NULL);
        if (!$max_consecutive_timespan || $last_message['time'] >= time() - $max_consecutive_timespan * 60) {

          // No email should be sent - stop function execution.
          return;
        }

        // Reset last message count if max consecutive time has already passed.
        $last_message['count'] = 0;
      }

      // Email should and will be sent, so increase counter for this message.
      $message_count = ++$last_message['count'];
    }
  }

  // Add additional debug info (PHP predefined variables, debug backtrace etc.)
  $log['debug_info'] = array();
  $debug_info_settings = variable_get('emaillog_debug_info', NULL);
  foreach (_emaillog_get_debug_info_callbacks() as $debug_info_key => $debug_info_callback) {
    if (isset($debug_info_settings[$log['severity']][$debug_info_key]) && $debug_info_settings[$log['severity']][$debug_info_key]) {
      eval('$log[\'debug_info\'][\'' . $debug_info_callback . '\'] = ' . $debug_info_callback . ';');
    }
  }
  drupal_alter('emaillog_debug_info', $log['debug_info']);

  // Make sure that $log['variables'] is always an array to avoid
  // errors like in issue http://drupal.org/node/1325938
  if (!is_array($log['variables'])) {
    $log['variables'] = array();
  }

  // Send email alert.
  drupal_mail('emaillog', 'alert', $to, $language->language, $log);

  // Update email repetition restricting variables if needed.
  if ($max_similar_emails) {
    $last_message = array(
      'message' => $log['type'] . $log['message'],
      'time' => time(),
      'count' => $message_count,
    );
    variable_set('emaillog_last_message', $last_message);
  }
}