ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
AmqpHandler.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Monolog\Handler;
13
16use PhpAmqpLib\Message\AMQPMessage;
17use PhpAmqpLib\Channel\AMQPChannel;
18use AMQPExchange;
19
21{
25 protected $exchange;
26
30 protected $exchangeName;
31
38 public function __construct($exchange, $exchangeName = 'log', $level = Logger::DEBUG, $bubble = true)
39 {
40 if ($exchange instanceof AMQPExchange) {
41 $exchange->setName($exchangeName);
42 } elseif ($exchange instanceof AMQPChannel) {
43 $this->exchangeName = $exchangeName;
44 } else {
45 throw new \InvalidArgumentException('PhpAmqpLib\Channel\AMQPChannel or AMQPExchange instance required');
46 }
47 $this->exchange = $exchange;
48
49 parent::__construct($level, $bubble);
50 }
51
55 protected function write(array $record)
56 {
57 $data = $record["formatted"];
58 $routingKey = $this->getRoutingKey($record);
59
60 if ($this->exchange instanceof AMQPExchange) {
61 $this->exchange->publish(
62 $data,
63 $routingKey,
64 0,
65 array(
66 'delivery_mode' => 2,
67 'content_type' => 'application/json',
68 )
69 );
70 } else {
71 $this->exchange->basic_publish(
73 $this->exchangeName,
74 $routingKey
75 );
76 }
77 }
78
82 public function handleBatch(array $records)
83 {
84 if ($this->exchange instanceof AMQPExchange) {
85 parent::handleBatch($records);
86
87 return;
88 }
89
90 foreach ($records as $record) {
91 if (!$this->isHandling($record)) {
92 continue;
93 }
94
95 $record = $this->processRecord($record);
96 $data = $this->getFormatter()->format($record);
97
98 $this->exchange->batch_basic_publish(
100 $this->exchangeName,
101 $this->getRoutingKey($record)
102 );
103 }
104
105 $this->exchange->publish_batch();
106 }
107
114 protected function getRoutingKey(array $record)
115 {
116 $routingKey = sprintf(
117 '%s.%s',
118 // TODO 2.0 remove substr call
119 substr($record['level_name'], 0, 4),
120 $record['channel']
121 );
122
123 return strtolower($routingKey);
124 }
125
130 private function createAmqpMessage($data)
131 {
132 return new AMQPMessage(
133 (string) $data,
134 array(
135 'delivery_mode' => 2,
136 'content_type' => 'application/json',
137 )
138 );
139 }
140
144 protected function getDefaultFormatter()
145 {
147 }
148}
An exception for terminatinating execution or to throw for unit testing.
Encodes whatever record data is passed to it as json.
getFormatter()
{Gets the formatter.FormatterInterface}
isHandling(array $record)
{Checks whether the given record will be handled by this handler.This is mostly done for performance ...
Base Handler class providing the Handler structure.
processRecord(array $record)
Processes a record.
handleBatch(array $records)
{Handles a set of records at once.}
Definition: AmqpHandler.php:82
write(array $record)
Writes the record down to the log of the implementing handler.void
Definition: AmqpHandler.php:55
__construct($exchange, $exchangeName='log', $level=Logger::DEBUG, $bubble=true)
Definition: AmqpHandler.php:38
getRoutingKey(array $record)
Gets the routing key for the AMQP exchange.
getDefaultFormatter()
Gets the default formatter.FormatterInterface
Monolog log channel.
Definition: Logger.php:29
const DEBUG
Detailed debug information.
Definition: Logger.php:33
$records
Definition: simple_test.php:22
$data
Definition: bench.php:6