-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathLoggerDomainEventSubscriber.php
58 lines (48 loc) · 1.63 KB
/
LoggerDomainEventSubscriber.php
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
56
57
58
<?php
namespace Lw\Domain\Event;
use Ddd\Domain\DomainEventSubscriber;
use Elastica\Client;
use JMS\Serializer\SerializerBuilder;
use Monolog\Handler\ElasticSearchHandler;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Processor\MemoryPeakUsageProcessor;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Processor\WebProcessor;
class LoggerDomainEventSubscriber implements DomainEventSubscriber
{
private $logger;
private $serializer;
public function __construct()
{
$this->logger = new Logger('main');
$this->logger->pushHandler(new StreamHandler('/tmp/app.log'));
$options = array(
'index' => 'last_wishes_logs',
'type' => 'log_entry',
);
$this->logger->pushHandler(new ElasticSearchHandler(new Client(), $options));
$this->logger->pushProcessor(new WebProcessor());
$this->logger->pushProcessor(new MemoryUsageProcessor());
$this->logger->pushProcessor(new MemoryPeakUsageProcessor());
$this->serializer = SerializerBuilder::create()->build();
}
public function handle($aDomainEvent)
{
$domainEventInArray = json_decode($this->serializer->serialize($aDomainEvent, 'json'), true);
try {
$this->logger->addInfo(
get_class($aDomainEvent),
$domainEventInArray + [
'name' => get_class($aDomainEvent),
'occurred_on' => $aDomainEvent->occurredOn(),
]
);
} catch (\Exception $e) {
}
}
public function isSubscribedTo($aDomainEvent)
{
return true;
}
}