Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (39)

View Page History
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
* @version $Id: Mail.php 217 2008-11-16 03:51:54Z brian $
*/


/**
* Class used for writing log messages to email via Zend_Mail.
*
* Allows for emailing log messages at and above a certain level via a
* Zend_Mail object. Note that this class only sends the email upon
* completion, so any log entries accumulated are sent in a single email.
*
* @category Zend
* @package Zend_Log
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
* @version $Id: Mail.php 217 2008-11-16 03:51:54Z brian $
*/
class Zend_Log_Writer_Mail extends Zend_Log_Writer_Abstract
* @var array
*/
private $_eventsToMail = array();

/**
* Array of formatted lines for use in an HTML email body; these events
* are formatted with an optional formatter if the caller is using
* Zend_Layout.
*
* @var array
*/
private $_layoutEventsToMail = array();

/**
* Zend_Mail instance to use
*

/**
* Zend_Layout instance to use; optional.
*
* @var Zend_Layout

/**
* Class constructor
* Optional formatter for use when rendering with Zend_Layout.
*
* @var Zend_Log_Formatter_Interface
*/
private $_layoutFormatter;

/**
* Array keeping track of the number of entries per priority level.
*
* @var array
*/
private $_numEntriesPerPriority = array();

/**
* Subject prepend text.
*
* Can only be used of the Zend_Mail object has not already had its
* subject line set. Using this will cause the subject to have the entry
* counts per-priority level appended to it.
*
* @var string|null
*/
private $_subjectPrependText;

/**
* Class constructor.
*
* Constructs the mail writer; requires a Zend_Mail instance, and takes an
* optional Zend_Layout instance. If Zend_Layout is being used,
* $this->_layout->events will be set for use in the layout template.
*
* @param Zend_Mail $mail Mail instance
* @param Zend_Layout $layout Layout instance; optional
* @return void
*/
public function __construct(Zend_Mail $mail, Zend_Layout $layout = null)
$this->_layout = $layout;
$this->_formatter = new Zend_Log_Formatter_Simple();
$this->_eventsToMail = array();
}

* Places event line into array of lines to be used as message body.
*
* Handles the formatting of both plaintext entries, as well as those
* rendered with Zend_Layout.
*
* @param array $event Event data
* @return void
protected function _write($event)
{
// Track the number of entries per priority level.
if (!isset($this->_numEntriesPerPriority[$event['priorityName']])) {
$this->_numEntriesPerPriority[$event['priorityName']] = 1;
} else {
$this->_numEntriesPerPriority[$event['priorityName']]++;
}

$formattedEvent = $this->_formatter->format($event);

// All plaintext events are to use the standard formatter.
$this->_eventsToMail[] = $this->_formatter->format($event); $formattedEvent;

// If we have a Zend_Layout instance, use a specific formatter for the
// layout if one exists. Otherwise, just use the event with its
// default format.
if ($this->_layout) {
if ($this->_layoutFormatter) {
$this->_layoutEventsToMail[] =
$this->_layoutFormatter->format($event);
} else {
$this->_layoutEventsToMail[] = $formattedEvent;
}
}
}

/**
* Sends mail to recipient(s) if log entries are present. Sets a specific formatter for use with Zend_Layout events.
*
* Allows use of a second formatter on lines that will be rendered with
* Zend_Layout. In the event that Zend_Layout is not being used, this
* formatter cannot be set, so an exception will be thrown.
*
* @param Zend_Log_Formatter_Interface $formatter
* @return void
* @throw Zend_Log_Exception
*/
public function setLayoutFormatter(Zend_Log_Formatter_Interface $formatter)
{
if (!$this->_layout) {
throw new Zend_Log_Exception(
'cannot set formatter for layout; ' .
'a Zend_Layout instance is not in use');
}

$this->_layoutFormatter = $formatter;
}

/**
* Allows caller to have the mail subject dynamically set to contain the
* entry counts per-priority level.
*
* Sets the text for use in the subject, with entry counts per-priority
* level appended to the end. Since a Zend_Mail subject can only be set
* once, this method cannot be used if the Zend_Mail object already has a
* subject set.
*
* @param string $subject Subject prepend text.
* @return void
*/
public function setSubjectPrependText($subject)
{
if ($this->_mail->getSubject()) {
throw new Zend_Log_Exception(
'subject already set on mail; ' .
'cannot set subject prepend text');
}

$this->_subjectPrependText = (string) $subject;
}

/**
* Sends mail to recipient(s) if log entries are present. Note that both
* plaintext and HTML portions of email are handled here.
*
* @return void
*/
public function shutdown()
{
// If there are events to mail, use them as message body. Otherwise,
// there is no mail to be sent.
if (!empty($this->_eventsToMail)) {
// Always provide events to mail as plaintext. return;
$this->_mail->setBodyText(implode("", $this->_eventsToMail)); }

if ($this->_subjectPrependText !== null) {
// Tack on the summary of entries per-priority to the subject
// line and set it on the Zend_Mail object.
// If a Zend_Layout instance exists, assume that it's for the HTML $numEntries = $this->_getFormattedNumEntriesPerPriority();
// portion of the message. $this->_mail->setSubject(
if ($this->_layout) { "{$this->_subjectPrependText} ({$numEntries})");
// Separate each line with a linebreak.
$bodyHtml = implode('<br/>', $this->_eventsToMail); }

// Set the required "messages" value for the layout and the
// message's HTML body.
$this->_layout->messages = $bodyHtml;
$this->_mail->setBodyHtml($this->_layout->render());
}

// Always provide events to mail as plaintext.
$this->_mail->setBodyText(implode('', $this->_eventsToMail));

// If a Zend_Layout instance is being used, set its "messages"
// value to the lines formatted for use with the layout.
if ($this->_layout) {
// Set the required "events" value for the layout. Here we
// are assuming that the layout is for use with HTML.
// Finally, send the mail, but re-throw any exceptions at the $this->_layout->events =
implode('', $this->_layoutEventsToMail);
// proper level of abstraction. $this->_mail->setBodyHtml($this->_layout->render());
try {
$this->_mail->send();
} catch (Exception $e) {
throw new Zend_Log_Exception(
$e->getMessage(),
$e->getCode());
}
}

// Finally, send the mail, but re-throw any exceptions at the
// proper level of abstraction.
try {
$this->_mail->send();
} catch (Exception $e) {
throw new Zend_Log_Exception(
$e->getMessage(),
$e->getCode());
}
}
}

/**
* Gets a string of number of entries per-priority level that occurred, or
* null if none occurred.
*
* @return string|null
*/
private function _getFormattedNumEntriesPerPriority()
{
if (empty($this->_numEntriesPerPriority)) {
return null;
}

$strings = array();

foreach ($this->_numEntriesPerPriority as $priority => $numEntries) {
$strings[] = "{$priority}={$numEntries}";
}

return implode(', ', $strings);
}
}
{code}
{zone-data}