function emaillog_watchdog in Logging and alerts 7.2
Same name and namespace in other branches
- 6.2 emaillog/emaillog.module \emaillog_watchdog()
- 6 emaillog/emaillog.module \emaillog_watchdog()
- 7 emaillog/emaillog.module \emaillog_watchdog()
Implements hook_watchdog().
File
- emaillog/
emaillog.module, line 61 - 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']))) {
return;
}
// Send if this is one of the specified types, or if no type was specified,
// otherwise the message is ignored by this module.
$types = variable_get('emaillog_' . $log['severity'] . '_types');
if (!empty($types)) {
if (strpos($types, '!') === 0) {
// Send for anything BUT these types.
$types = substr($types, 1);
if (in_array($log['type'], array_filter(explode(',', $types), 'trim'))) {
return;
}
}
else {
// Send ONLY for these types.
if (!in_array($log['type'], array_filter(explode(',', $types), 'trim'))) {
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');
if ($max_similar_emails) {
$max_similarity_level = variable_get('emaillog_max_similarity_level');
// Get previously sent message data and compare its content with current one.
$last_message = variable_get('emaillog_last_message');
$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');
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');
foreach (_emaillog_get_debug_info_callbacks() as $debug_info_key => $debug_info_callback) {
if (!empty($debug_info_settings[$log['severity']][$debug_info_key]) && function_exists($debug_info_callback['callback'])) {
$log['debug_info'][$debug_info_callback['label']] = call_user_func_array($debug_info_callback['callback'], $debug_info_callback['arguments']);
}
}
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.
if (!function_exists('drupal_mail')) {
// If drupal isn't fully bootstrapped get the drupal_mail(),
// drupal_ucfirst(), and theme() functions.
include_once DRUPAL_ROOT . '/includes/mail.inc';
include_once DRUPAL_ROOT . '/includes/unicode.inc';
include_once DRUPAL_ROOT . '/includes/theme.inc';
}
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);
}
}