Skip to content

Instantly share code, notes, and snippets.

@frederikbosch
Last active August 29, 2015 14:15
Show Gist options
  • Save frederikbosch/eaa21b844a1523c4e3b2 to your computer and use it in GitHub Desktop.
Save frederikbosch/eaa21b844a1523c4e3b2 to your computer and use it in GitHub Desktop.
Xmpp Logger Monolog
<?php
use Monolog\Handler\AbstractProcessingHandler;
use Fabiang\Xmpp\Client;
use Fabiang\Xmpp\Options as XmppOptions;
use Fabiang\Xmpp\Protocol\Roster;
use Fabiang\Xmpp\Protocol\Presence;
use Fabiang\Xmpp\Protocol\Message;
use Monolog\Logger;
class XmppLogger extends AbstractProcessingHandler {
private $stream;
private $username;
private $password;
private $client;
private $channel;
private $nickname = 'Error Logger';
public function __construct($stream, $username, $password, $channel, $level = Logger::DEBUG, $bubble = true) {
parent::__construct($level, $bubble);
$this->stream = $stream;
$this->username = $username;
$this->password = $password;
$this->channel = $channel;
}
private function joinChannelIfNotJoined () {
if ($this->client === null) {
$options = new XmppOptions($this->stream);
$options->setUsername($this->username);
$options->setPassword($this->password);
$this->client = new Client($options);
$this->client->connect();
// fetch roster list; users and their groups
$this->client->send(new Roster());
// set status to online
$this->client->send(new Presence());
// join a channel
$channel = new Presence;
$channel->setTo($this->channel);
$channel->setNickName($this->nickname);
$this->client->send($channel);
}
}
protected function write (array $record) {
$this->joinChannelIfNotJoined();
// send a message to the above channel
$message = new Message();
$message->setMessage((string) $record['formatted']);
$message->setTo($this->channel);
$message->setType(Message::TYPE_GROUPCHAT);
$this->client->send($message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment