You are here

emaillog.module in Logging and alerts 7

Drupal Module: Email Logging and Alerts

Sends logs and alerts to email addresses.

@Author: Khalid Baheyeldin http://2bits.com (version 6.x) @Author: Maciej Zgadzaj http://zgadzaj.com (version 7.x)

File

emaillog/emaillog.module
View source
<?php

/**
 * @file
 * Drupal Module: Email Logging and Alerts
 *
 * Sends logs and alerts to email addresses.
 *
 * @Author: Khalid Baheyeldin http://2bits.com (version 6.x)
 * @Author: Maciej Zgadzaj http://zgadzaj.com (version 7.x)
 */

/**
 * Implements hook_help().
 */
function emaillog_help($path, $arg) {
  switch ($path) {
    case 'admin/help#emaillog':
      return '<p>' . t('Sends logs and alerts to email addresses, with different severity going to different emails.') . '</p>';
    case 'admin/reports/dblog':
      return '<p>' . t('Sends logs and alerts to email addresses, with different severity going to different emails.') . '</p>';
  }
}

/**
 * Implements hook_menu().
 */
function emaillog_menu() {
  $items['admin/config/system/emaillog'] = array(
    'title' => 'Email logging and alerts',
    'description' => 'Settings for logging and alerts to email addresses.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'emaillog_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'emaillog.admin.inc',
  );
  return $items;
}
function emaillog_watchdog($log) {
  $to = variable_get('emaillog_' . $log['severity'], '');
  if ($to) {

    // Send email only if there is an email address.
    // Otherwise the message is ignored by this module.
    $language = user_preferred_language($log['user']);
    drupal_mail('emaillog', 'alert', $to, $language, $log);
  }
}

/**
 * Implements hook_mail().
 */
function emaillog_mail($key, &$message, $params) {
  switch ($key) {
    case 'alert':
      $severity_list = watchdog_severity_levels();
      $message['subject'] = t('[@site_name] @severity_desc: @dblog_message', array(
        '@site_name' => variable_get('site_name', 'Drupal'),
        '@severity_desc' => drupal_strtoupper($severity_list[$params['severity']]),
        '@dblog_message' => _emaillog_format_dblog_message($params),
      ));
      $message['body'][] = theme('emaillog_format', $params);
      break;
  }
}

/**
 * Implements hook_theme().
 */
function emaillog_theme() {
  return array(
    'emaillog_format' => array(
      'arguments' => array(
        'log_msg' => NULL,
      ),
    ),
  );
}

/**
 * Themes email body.
 */
function theme_emaillog_format($log_msg = array()) {
  global $base_url;
  $severity_list = watchdog_severity_levels();
  $message = "\nSite:         @base_url";
  $message .= "\nSeverity:     (@severity) @severity_desc";
  $message .= "\nTimestamp:    @timestamp";
  $message .= "\nType:         @type";
  $message .= "\nIP Address:   @ip";
  $message .= "\nRequest URI:  @request_uri";
  $message .= "\nReferrer URI: @referer_uri";
  $message .= "\nUser:         (@uid) @name";
  $message .= "\nLink:         @link";
  $message .= "\nMessage:      \n\n@message";
  $message = t($message, array(
    '@base_url' => $base_url,
    '@severity' => $log_msg['severity'],
    '@severity_desc' => drupal_ucfirst($severity_list[$log_msg['severity']]),
    '@timestamp' => format_date($log_msg['timestamp']),
    '@type' => $log_msg['type'],
    '@ip' => $log_msg['ip'],
    '@request_uri' => $log_msg['request_uri'],
    '@referer_uri' => $log_msg['referer'],
    '@uid' => $log_msg['user']->uid,
    '@name' => format_username($log_msg['user']),
    '@link' => strip_tags($log_msg['link']),
    '@message' => strip_tags(t($log_msg['message'], $log_msg['variables'])),
  ));
  return $message;
}

/**
 * Injects variables into dblog message and formats it for email subject.
 */
function _emaillog_format_dblog_message($event) {
  if (empty($event['variables'])) {
    $message = strip_tags($event['message']);
  }
  else {
    $message = strip_tags(t($event['message'], $event['variables']));
  }
  return truncate_utf8($message, 60, TRUE, TRUE);
}

Functions

Namesort descending Description
emaillog_help Implements hook_help().
emaillog_mail Implements hook_mail().
emaillog_menu Implements hook_menu().
emaillog_theme Implements hook_theme().
emaillog_watchdog
theme_emaillog_format Themes email body.
_emaillog_format_dblog_message Injects variables into dblog message and formats it for email subject.