1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-04 20:27:31 +02:00

amqp handler.. need to test

This commit is contained in:
pomaxa
2012-06-12 16:48:32 +03:00
parent 2eb0c0978d
commit b4a33da363

View File

@@ -0,0 +1,50 @@
<?php
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Formatter\NormalizerFormatter;
use Monolog\Formatter\JsonFormatter;
/**
* handle sending logs to the rabbitmq, using Amqp protocol;
*
* @author pomaxa none <pomaxa@gmail.com>
*/
class AmqpHandler extends AbstractProcessingHandler
{
protected $exchange;
protected $space;
function __construct(\AMQPConnection $amqp, $exchange = 'log', $space = '', $level = Logger::DEBUG, $bubble = true)
{
$channel = new \AMQPChannel($amqp);
$this->exchange = new \AMQPExchange($channel);
$this->exchange->setName($exchange);
parent::__construct($level, $bubble);
}
/**
* Writes the record down to the log of the implementing handler
*
* @param array $record
* @return void
*/
protected function write(array $record)
{
$data = json_encode($record["formatted"]);
$routingKey = substr(strtolower($record['level_name']),0,4 ).'.'.$this->space;
$this->exchange->publish($data, $routingKey, 0,
array('delivery_mode' => 2, 'Content-type' => 'application/json'));
}
/**
* {@inheritDoc}
*/
protected function getDefaultFormatter()
{
return new NormalizerFormatter();
}
}