Skip to content

Instantly share code, notes, and snippets.

@eleclerc
Created December 2, 2009 01:28
Show Gist options
  • Save eleclerc/246840 to your computer and use it in GitHub Desktop.
Save eleclerc/246840 to your computer and use it in GitHub Desktop.
Zend Log Writer XMPP (gtalk)
<?php
/**
* Writter for Zend_Log that use the XMPP protocol.
*
* This class depends on XMPPHP library from http://code.google.com/p/xmpphp
* Note: The recipient must have added the sender to his roster/contactlist
* in order for him to receive the message
*
* @category Danceric
* @package Danceric_Log
* @subpackage Writer
*/
/** Zend_Log_Writer_Abstract */
require_once 'Zend/Log/Writer/Abstract.php';
/** Zend_Log_Exception */
require_once 'Zend/Log/Exception.php';
/** Zend_Log_Formatter_Simple*/
require_once 'Zend/Log/Formatter/Simple.php';
/** XMPPHP_XMPP */
require 'XMPPHP/XMPP.php';
/**
* Class used for writing log messages to xmpp via XMPPHP.
*
* Note that this class only sends the email upon
* completion, so any log entries accumulated are sent in a single message.
*
* @category Mmsr
* @package Danceric_Log
* @subpackage Writer
*/
class Danceric_Log_Writer_Xmpp extends Zend_Log_Writer_Abstract
{
/**
* Array of formatted events to include in message body.
*
* @var array
*/
protected $_eventsToSend = array();
/**
* Array of xmpp connection information. default to gtalk/gmail info
*
* @var array
*/
public $options = array(
'host' => 'talk.google.com',
'port' => 5222,
'user' => '',
'password' => '',
'resource' => 'xmpphp',
'server' => 'gmail.com',
'recipient' => '');
/**
* @param array $options xmpp connection information, mandatory: user, password, recipient
* @return void
*/
public function __construct($options)
{
$this->options = $options;
$this->setFormatter(new Zend_Log_Formatter_Simple());
}
/**
* Places event line into array of lines to be used as message body.
*
*
* @param array $event Event data
* @return void
*/
protected function _write($event)
{
$formattedEvent = $this->_formatter->format($event);
$this->_eventsToSend[] = $formattedEvent;
}
/**
* Sends message recipient if log entries are present.
*
* @return void
*/
public function shutdown()
{
// If there are events to send, use them as message body.
// Otherwise, there is no message to be sent.
if (empty($this->_eventsToSend)) {
return;
}
$events = implode('', $this->_eventsToSend);
// Finally, send the IM, but re-throw any exceptions at the
// proper level of abstraction.
try {
$conn = new XMPPHP_XMPP($this->options['host'],
$this->$options['port'],
$this->$options['user'],
$this->$options['password'],
$this->$options['resource'],
$this->$options['server']);
$conn->connect();
$conn->processUntil('session_start');
$conn->presence();
$conn->message($this->options['recipient'], $events);
$conn->disconnect();
} catch (Exception $e) {
throw new Zend_Log_Exception(
$e->getMessage(),
$e->getCode());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment