Skip to content

Instantly share code, notes, and snippets.

@johannilsson
Created March 20, 2009 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johannilsson/82394 to your computer and use it in GitHub Desktop.
Save johannilsson/82394 to your computer and use it in GitHub Desktop.
<?php
// Example tweets,
// Long message http://twitter.com/johantester/status/1364993887
// Normal message http://twitter.com/johantester/status/1364993866
class TwitterLogWriter extends Zend_Log_Writer_Abstract {
private $_twitter = null;
public function __construct($username, $password) {
$this->_twitter = new Zend_Service_Twitter($username, $password);
$this->_formatter = new Zend_Log_Formatter_Simple('%priorityName%: %message%');
}
protected function _write($event) {
$line = $this->_filterLine($this->_formatter->format($event));
try {
$this->_twitter->status->update($line);
} catch (Zend_Http_Client_Exception $e) {
; // We failed to post, log failure, put it on a queue or just dont care.
}
}
protected function _filterLine($line) {
// You probably want to add some fancy filter chain here that
// removed sensitive data like email and so if you sending to
// a public stream.
if (($len = iconv_strlen($line, 'UTF-8')) > 140) {
$line = iconv_substr($line, 0, 137, 'UTF-8');
$line .= "...";
}
return $line;
}
}
$logger = new Zend_Log();
$logger->addWriter(new Zend_Log_Writer_Stream('php://output'));
$logger->addWriter(new TwitterLogWriter('username', 'password'));
$logger->log('Informational message', Zend_Log::INFO);
$logger->log('Long message Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quam risus, auctor in, tincidunt ac, cursus ut, ante. Fusce risus nulla, viverra ut, faucibus nec, consequat et, quam.', Zend_Log::DEBUG);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment