You are here

emaillog.module in Logging and alerts 6

Sends logs and alerts to email addresses.

File

emaillog/emaillog.module
View source
<?php

// Copyright 2007 Khalid Baheyeldin http://2bits.com

/**
 * @file
 * Sends logs and alerts to email addresses.
 */

/**
 * Implementation of hook_help().
 */
function emaillog_help($path, $arg) {
  switch ($path) {
    case 'admin/help#emaillog':
    case 'admin/settings/logging/emaillog':
      return '<p>' . t('Sends logs and alerts to email addresses.') . '</p>';
  }
}

/**
 * Implementation of hook_menu().
 */
function emaillog_menu() {
  $items['admin/settings/logging/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',
    ),
    'file' => 'emaillog.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_watchdog().
 */
function emaillog_watchdog($log) {
  global $user;
  $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($user);
    drupal_mail('emaillog', 'alert', $to, $language, $log);
  }
}

/**
 * Implementation of hook_mail().
 */
function emaillog_mail($key, &$message, $params) {
  if ($key == 'alert') {
    $severity_levels = 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_levels[$params['severity']]),
      '@dblog_message' => _emaillog_format_dblog_message($params),
    ));
    $message['body'] = theme('emaillog_format', $params, $message);
  }
}

/**
 * Implementation of hook_theme().
 */
function emaillog_theme() {
  return array(
    'emaillog_format' => array(
      'arguments' => array(
        'log_msg' => NULL,
      ),
    ),
  );
}
function theme_emaillog_format($log_msg = array()) {
  global $base_url;
  $severity_levels = 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_levels[$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' => !empty($log_msg['user']->name) ? $log_msg['user']->name : variable_get('anonymous', t('Anonymous')),
    '@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 Implementation of hook_help().
emaillog_mail Implementation of hook_mail().
emaillog_menu Implementation of hook_menu().
emaillog_theme Implementation of hook_theme().
emaillog_watchdog Implementation of hook_watchdog().
theme_emaillog_format
_emaillog_format_dblog_message Injects variables into dblog message and formats it for email subject.