Skip to content

Instantly share code, notes, and snippets.

@ericclemmons
Created July 7, 2010 20:29
Show Gist options
  • Save ericclemmons/467226 to your computer and use it in GitHub Desktop.
Save ericclemmons/467226 to your computer and use it in GitHub Desktop.
Have Zend_Log_Writer_Mail send you entire error backlog
resources.log.mail.writerName = "Backlog"
resources.log.mail.writerNamespace = "My_Log_Writer"
resources.log.mail.writerParams.from = "errors@email.com"
resources.log.mail.writerParams.to = "my@email.com"
resources.log.mail.writerParams.subject = "[Errors]"
resources.log.mail.writerParams.transport.url = "smtp.gmail.com"
resources.log.mail.writerParams.transport.params.ssl = "ssl"
resources.log.mail.writerParams.transport.params.auth = "login"
resources.log.mail.writerParams.transport.params.username = "errors@email.com"
resources.log.mail.writerParams.transport.params.password = "p@s$w0rd"
resources.log.mail.writerParams.transport.params.port = 465
<?php
class Forms_Log_Writer_Backlog extends \Zend_Log_Writer_Mail
{
/**
* Array of all events sent to writer, unfiltered.
*
* @var array
*/
protected $_eventHistory = array();
/**
* Create a new instance of Zend_Log_Writer_Mail
*
* @param array|Zend_Config $config
* @return Zend_Log_Writer_Mail
* @throws Zend_Log_Exception
*/
public static function factory($config)
{
$transport = new Zend_Mail_Transport_Smtp($config['transport']['url'],
$config['transport']['params']);
$mail = new Zend_Mail();
$mail->setFrom($config['from'])
->addTo($config['to'])
->setDefaultTransport($transport);
$writer = new self($mail);
$writer->setSubjectPrependText($config['subject'])
->addFilter(Zend_Log::ERR);
return $writer;
}
/**
* Returns an array of all of the events sent to the writer thus far
*
* @return Array
*/
public function getEventHistory()
{
return $this->_eventHistory;
}
/**
* Log a message to this writer.
*
* @param array $event log data event
* @return void
*/
public function write($event)
{
$this->_eventHistory[] = $event;
return parent::write($event);
}
/**
* 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)) {
return;
}
// Now add the backlog of messages for inclusion in the email
foreach ($this->getEventHistory() as $event) {
$this->_write($event);
}
return parent::shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment