1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-22 09:06:10 +02:00
Files
php-monolog/src/Monolog/Handler/AmqpHandler.php
pomaxa be762cb0c5 style
2012-06-13 11:21:22 +03:00

60 lines
1.7 KiB
PHP

<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Formatter\JsonFormatter;
class AmqpHandler extends AbstractProcessingHandler
{
protected $exchange;
protected $space;
/**
* @param \AMQPConnection $amqp Amqp connection, ready for use
* @param string $exchange exchange name
* @param string $space string to be able better manage routing keys
* @param int $level
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
*/
function __construct(\AMQPConnection $amqp, $exchange = 'log', $space = '', $level = Logger::DEBUG, $bubble = true)
{
$this->space = $space;
$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 = $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 JsonFormatter();
}
}