Skip to content

Commit 217a9a5

Browse files
committed
[doc][skip ci] Add docs for monolog handler.
1 parent bb183aa commit 217a9a5

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎docs/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* [Use cases](#use-cases)
4646
- [FOSElasticaBundle. Populate command optimizations](elastica-bundle/populate-command-optimization.md)
4747
- [Symfony. Async event dispatcher](async_event_dispatcher/quick_tour.md)
48+
- [Monolog. Send messages to message queue](monolog/send-messages-to-mq.md)
4849
* [Development](#development)
4950
- [Contribution](contribution.md)
5051

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Enqueue Monolog Handlers
2+
3+
The package provides handlers for [Monolog](https://github.com/Seldaek/monolog).
4+
These handler allows to send logs to MQ using any [queue-interop](https://github.com/queue-interop/queue-interop) compatible transports.
5+
6+
## Installation
7+
8+
You have to install monolog itself, queue interop handlers and one of [the transports](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/index.md#transports).
9+
For the simplicity we are going to install the filesystem based MQ.
10+
11+
```
12+
composer require enqueue/monolog-queue-handler monolog/monlog enqueue/fs
13+
```
14+
15+
## Usage
16+
17+
```php
18+
<?php
19+
20+
use Monolog\Handler\QueueInteropHandler;
21+
use Monolog\Logger;
22+
23+
require_once __DIR__.'/vendor/autoload.php';
24+
25+
$context = (new \Enqueue\Fs\FsConnectionFactory('file://'.__DIR__.'/queue'))->createContext();
26+
27+
// create a log channel
28+
$log = new Logger('name');
29+
$log->pushHandler(new QueueInteropHandler($context));
30+
31+
// add records to the log
32+
$log->warning('Foo');
33+
$log->error('Bar');
34+
```
35+
36+
the consumer may look like this:
37+
38+
```php
39+
<?php
40+
41+
use Enqueue\Consumption\QueueConsumer;
42+
use Interop\Queue\PsrMessage;
43+
use Interop\Queue\PsrProcessor;
44+
45+
require_once __DIR__.'/vendor/autoload.php';
46+
47+
$context = (new \Enqueue\Fs\FsConnectionFactory('file://'.__DIR__.'/queue'))->createContext();
48+
49+
$consumer = new QueueConsumer($context);
50+
$consumer->bind('log', function(PsrMessage $message) {
51+
echo $message->getBody().PHP_EOL;
52+
53+
return PsrProcessor::ACK;
54+
});
55+
56+
$consumer->consume();
57+
58+
```
59+
60+
[back to index](../index.md)

0 commit comments

Comments
 (0)