-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathamqp.php
More file actions
55 lines (43 loc) · 1.47 KB
/
amqp.php
File metadata and controls
55 lines (43 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/*
* This file is part of the php-gelf package.
*
* (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__ . '/../vendor/autoload.php';
// Note, You need to install "amqp" extension for PHP.
// @link http://php.net/manual/pl/book.amqp.php
$connection = new \AMQPConnection(array(
'host' => 'localhost',
'login' => '',
'password' => ''
));
$connection->connect();
$channel = new \AMQPChannel($connection);
$exchange = new \AMQPExchange($channel);
$exchange->setName('log-messages');
$exchange->setType(AMQP_EX_TYPE_FANOUT);
$exchange->declareExchange();
$queue = new \AMQPQueue($channel);
$queue->setName('log-messages');
$queue->setFlags(AMQP_DURABLE);
$queue->declareQueue();
$queue->bind($exchange->getName());
$transport = new Gelf\Transport\AmqpTransport($exchange, $queue);
$publisher = new Gelf\Publisher();
$publisher->addTransport($transport);
// Now we can create custom messages and publish them
$message = new Gelf\Message();
$message->setShortMessage("Foobar!")
->setLevel(\Psr\Log\LogLevel::ALERT)
->setFullMessage("There was a foo in bar")
;
$publisher->publish($message);
// The implementation of PSR-3 is encapsulated in the Logger-class.
// It provides high-level logging methods, such as alert(), info(), etc.
$logger = new Gelf\Logger($publisher);
// Now we can log...
$logger->alert("Foobaz!");