Last active
August 29, 2015 14:15
-
-
Save frederikbosch/eaa21b844a1523c4e3b2 to your computer and use it in GitHub Desktop.
Xmpp Logger Monolog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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