00001 <?php
00024 require_once 'Zend/Log/Writer/Abstract.php';
00025
00027 require_once 'Zend/Log/Exception.php';
00028
00030 require_once 'Zend/Log/Formatter/Simple.php';
00031
00046 class Zend_Log_Writer_Mail extends Zend_Log_Writer_Abstract
00047 {
00053 protected $_eventsToMail = array();
00054
00062 protected $_layoutEventsToMail = array();
00063
00069 protected $_mail;
00070
00076 protected $_layout;
00077
00083 protected $_layoutFormatter;
00084
00090 protected $_numEntriesPerPriority = array();
00091
00101 protected $_subjectPrependText;
00102
00114 public function __construct(Zend_Mail $mail, Zend_Layout $layout = null)
00115 {
00116 $this->_mail = $mail;
00117 $this->_layout = $layout;
00118 $this->_formatter = new Zend_Log_Formatter_Simple();
00119 }
00120
00130 protected function _write($event)
00131 {
00132
00133 if (!isset($this->_numEntriesPerPriority[$event['priorityName']])) {
00134 $this->_numEntriesPerPriority[$event['priorityName']] = 1;
00135 } else {
00136 $this->_numEntriesPerPriority[$event['priorityName']]++;
00137 }
00138
00139 $formattedEvent = $this->_formatter->format($event);
00140
00141
00142 $this->_eventsToMail[] = $formattedEvent;
00143
00144
00145
00146
00147 if ($this->_layout) {
00148 if ($this->_layoutFormatter) {
00149 $this->_layoutEventsToMail[] =
00150 $this->_layoutFormatter->format($event);
00151 } else {
00152 $this->_layoutEventsToMail[] = $formattedEvent;
00153 }
00154 }
00155 }
00156
00163 public function getLayoutFormatter()
00164 {
00165 return $this->_layoutFormatter;
00166 }
00167
00179 public function setLayoutFormatter(Zend_Log_Formatter_Interface $formatter)
00180 {
00181 if (!$this->_layout) {
00182 throw new Zend_Log_Exception(
00183 'cannot set formatter for layout; ' .
00184 'a Zend_Layout instance is not in use');
00185 }
00186
00187 $this->_layoutFormatter = $formatter;
00188 return $this;
00189 }
00190
00203 public function setSubjectPrependText($subject)
00204 {
00205 if ($this->_mail->getSubject()) {
00206 throw new Zend_Log_Exception(
00207 'subject already set on mail; ' .
00208 'cannot set subject prepend text');
00209 }
00210
00211 $this->_subjectPrependText = (string) $subject;
00212 return $this;
00213 }
00214
00221 public function shutdown()
00222 {
00223
00224
00225 if (empty($this->_eventsToMail)) {
00226 return;
00227 }
00228
00229 if ($this->_subjectPrependText !== null) {
00230
00231
00232 $numEntries = $this->_getFormattedNumEntriesPerPriority();
00233 $this->_mail->setSubject(
00234 "{$this->_subjectPrependText} ({$numEntries})");
00235 }
00236
00237
00238
00239 $this->_mail->setBodyText(implode('', $this->_eventsToMail));
00240
00241
00242
00243 if ($this->_layout) {
00244
00245
00246 $this->_layout->events =
00247 implode('', $this->_layoutEventsToMail);
00248
00249
00250
00251 try {
00252 $this->_mail->setBodyHtml($this->_layout->render());
00253 } catch (Exception $e) {
00254 trigger_error(
00255 "exception occurred when rendering layout; " .
00256 "unable to set html body for message; " .
00257 "message = {$e->getMessage()}; " .
00258 "code = {$e->getCode()}; " .
00259 "exception class = " . get_class($e),
00260 E_USER_NOTICE);
00261 }
00262 }
00263
00264
00265
00266
00267 try {
00268 $this->_mail->send();
00269 } catch (Exception $e) {
00270 trigger_error(
00271 "unable to send log entries via email; " .
00272 "message = {$e->getMessage()}; " .
00273 "code = {$e->getCode()}; " .
00274 "exception class = " . get_class($e),
00275 E_USER_WARNING);
00276 }
00277 }
00278
00285 protected function _getFormattedNumEntriesPerPriority()
00286 {
00287 $strings = array();
00288
00289 foreach ($this->_numEntriesPerPriority as $priority => $numEntries) {
00290 $strings[] = "{$priority}={$numEntries}";
00291 }
00292
00293 return implode(', ', $strings);
00294 }
00295 }