7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
23 /** Zend_Log_Writer_Abstract */
24 require_once 'Zend/Log/Writer/Abstract.php';
26 /** Zend_Log_Exception */
27 require_once 'Zend/Log/Exception.php';
29 /** Zend_Log_Formatter_Simple*/
30 require_once 'Zend/Log/Formatter/Simple.php';
33 * Class used for writing log messages to email via Zend_Mail.
35 * Allows for emailing log messages at and above a certain level via a
36 * Zend_Mail object. Note that this class only sends the email upon
37 * completion, so any log entries accumulated are sent in a single email.
42 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
43 * @license http://framework.zend.com/license/new-bsd New BSD License
46 class Zend_Log_Writer_Mail
extends Zend_Log_Writer_Abstract
49 * Array of formatted events to include in message body.
53 protected $_eventsToMail = array();
56 * Array of formatted lines for use in an HTML email body; these events
57 * are formatted with an optional formatter if the caller is using
62 protected $_layoutEventsToMail = array();
65 * Zend_Mail instance to use
72 * Zend_Layout instance to use; optional.
79 * Optional formatter for use when rendering with Zend_Layout.
81 * @var Zend_Log_Formatter_Interface
83 protected $_layoutFormatter;
86 * Array keeping track of the number of entries per priority level.
90 protected $_numEntriesPerPriority = array();
93 * Subject prepend text.
95 * Can only be used of the Zend_Mail object has not already had its
96 * subject line set. Using this will cause the subject to have the entry
97 * counts per-priority level appended to it.
101 protected $_subjectPrependText;
106 * Constructs the mail writer; requires a Zend_Mail instance, and takes an
107 * optional Zend_Layout instance. If Zend_Layout is being used,
108 * $this->_layout->events will be set for use in the layout template.
110 * @param Zend_Mail $mail Mail instance
111 * @param Zend_Layout $layout Layout instance; optional
114 public function __construct(Zend_Mail
$mail, Zend_Layout
$layout = null)
116 $this->_mail
= $mail;
117 $this->_layout
= $layout;
118 $this->_formatter
= new Zend_Log_Formatter_Simple();
122 * Create a new instance of Zend_Log_Writer_Mail
124 * @param array|Zend_Config $config
125 * @return Zend_Log_Writer_Mail
126 * @throws Zend_Log_Exception
128 static public function factory($config)
130 throw new Zend_Exception('Zend_Log_Writer_Mail does not currently implement a factory');
134 * Places event line into array of lines to be used as message body.
136 * Handles the formatting of both plaintext entries, as well as those
137 * rendered with Zend_Layout.
139 * @param array $event Event data
142 protected function _write($event)
144 // Track the number of entries per priority level.
145 if (!isset($this->_numEntriesPerPriority
[$event['priorityName']])) {
146 $this->_numEntriesPerPriority
[$event['priorityName']] = 1;
148 $this->_numEntriesPerPriority
[$event['priorityName']]++
;
151 $formattedEvent = $this->_formatter
->format($event);
153 // All plaintext events are to use the standard formatter.
154 $this->_eventsToMail
[] = $formattedEvent;
156 // If we have a Zend_Layout instance, use a specific formatter for the
157 // layout if one exists. Otherwise, just use the event with its
159 if ($this->_layout
) {
160 if ($this->_layoutFormatter
) {
161 $this->_layoutEventsToMail
[] =
162 $this->_layoutFormatter
->format($event);
164 $this->_layoutEventsToMail
[] = $formattedEvent;
170 * Gets instance of Zend_Log_Formatter_Instance used for formatting a
171 * message using Zend_Layout, if applicable.
173 * @return Zend_Log_Formatter_Interface|null The formatter, or null.
175 public function getLayoutFormatter()
177 return $this->_layoutFormatter
;
181 * Sets a specific formatter for use with Zend_Layout events.
183 * Allows use of a second formatter on lines that will be rendered with
184 * Zend_Layout. In the event that Zend_Layout is not being used, this
185 * formatter cannot be set, so an exception will be thrown.
187 * @param Zend_Log_Formatter_Interface $formatter
188 * @return Zend_Log_Writer_Mail
189 * @throws Zend_Log_Exception
191 public function setLayoutFormatter(Zend_Log_Formatter_Interface
$formatter)
193 if (!$this->_layout
) {
194 throw new Zend_Log_Exception(
195 'cannot set formatter for layout; ' .
196 'a Zend_Layout instance is not in use');
199 $this->_layoutFormatter
= $formatter;
204 * Allows caller to have the mail subject dynamically set to contain the
205 * entry counts per-priority level.
207 * Sets the text for use in the subject, with entry counts per-priority
208 * level appended to the end. Since a Zend_Mail subject can only be set
209 * once, this method cannot be used if the Zend_Mail object already has a
212 * @param string $subject Subject prepend text.
213 * @return Zend_Log_Writer_Mail
215 public function setSubjectPrependText($subject)
217 if ($this->_mail
->getSubject()) {
218 throw new Zend_Log_Exception(
219 'subject already set on mail; ' .
220 'cannot set subject prepend text');
223 $this->_subjectPrependText
= (string) $subject;
228 * Sends mail to recipient(s) if log entries are present. Note that both
229 * plaintext and HTML portions of email are handled here.
233 public function shutdown()
235 // If there are events to mail, use them as message body. Otherwise,
236 // there is no mail to be sent.
237 if (empty($this->_eventsToMail
)) {
241 if ($this->_subjectPrependText
!== null) {
242 // Tack on the summary of entries per-priority to the subject
243 // line and set it on the Zend_Mail object.
244 $numEntries = $this->_getFormattedNumEntriesPerPriority();
245 $this->_mail
->setSubject(
246 "{$this->_subjectPrependText} ({$numEntries})");
250 // Always provide events to mail as plaintext.
251 $this->_mail
->setBodyText(implode('', $this->_eventsToMail
));
253 // If a Zend_Layout instance is being used, set its "events"
254 // value to the lines formatted for use with the layout.
255 if ($this->_layout
) {
256 // Set the required "messages" value for the layout. Here we
257 // are assuming that the layout is for use with HTML.
258 $this->_layout
->events
=
259 implode('', $this->_layoutEventsToMail
);
261 // If an exception occurs during rendering, convert it to a notice
262 // so we can avoid an exception thrown without a stack frame.
264 $this->_mail
->setBodyHtml($this->_layout
->render());
265 } catch (Exception
$e) {
267 "exception occurred when rendering layout; " .
268 "unable to set html body for message; " .
269 "message = {$e->getMessage()}; " .
270 "code = {$e->getCode()}; " .
271 "exception class = " . get_class($e),
276 // Finally, send the mail. If an exception occurs, convert it into a
277 // warning-level message so we can avoid an exception thrown without a
280 $this->_mail
->send();
281 } catch (Exception
$e) {
283 "unable to send log entries via email; " .
284 "message = {$e->getMessage()}; " .
285 "code = {$e->getCode()}; " .
286 "exception class = " . get_class($e),
292 * Gets a string of number of entries per-priority level that occurred, or
293 * an emptry string if none occurred.
297 protected function _getFormattedNumEntriesPerPriority()
301 foreach ($this->_numEntriesPerPriority
as $priority => $numEntries) {
302 $strings[] = "{$priority}={$numEntries}";
305 return implode(', ', $strings);