1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Facile\MongoDbBundle\Services\Loggers; |
6
|
|
|
|
7
|
|
|
use Facile\MongoDbBundle\Services\Loggers\Model\LogEvent; |
8
|
|
|
use Symfony\Component\Asset\Exception\LogicException; |
9
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class MongoLogger |
13
|
|
|
*/ |
14
|
|
|
class MongoLogger implements DataCollectorLoggerInterface |
15
|
|
|
{ |
16
|
|
|
/** @var \SplQueue|LogEvent[] */ |
17
|
|
|
private $logs; |
18
|
|
|
|
19
|
|
|
/** @var array|string[] */ |
20
|
|
|
private $connections; |
21
|
|
|
|
22
|
|
|
/** @var Stopwatch */ |
23
|
|
|
private $stopwatch; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* MongoLogger constructor. |
27
|
|
|
* |
28
|
|
|
* @param Stopwatch $stopwatch |
29
|
|
|
*/ |
30
|
2 |
|
public function __construct(Stopwatch $stopwatch) |
31
|
|
|
{ |
32
|
2 |
|
$this->logs = new \SplQueue(); |
33
|
2 |
|
$this->connections = []; |
34
|
2 |
|
$this->stopwatch = $stopwatch; |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
public function startLogging() |
38
|
|
|
{ |
39
|
|
|
$this->stopwatch->start('mongodb_query_log'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $connection |
44
|
|
|
*/ |
45
|
2 |
|
public function addConnection(string $connection) |
46
|
|
|
{ |
47
|
2 |
|
$this->connections[] = $connection; |
48
|
2 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array|\string[] |
52
|
|
|
*/ |
53
|
|
|
public function getConnections(): array |
54
|
|
|
{ |
55
|
|
|
return $this->connections; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param LogEvent $event |
60
|
|
|
*/ |
61
|
|
|
public function logQuery(LogEvent $event) |
62
|
|
|
{ |
63
|
|
|
$watch = $this->stopwatch->stop('mongodb_query_log'); |
64
|
|
|
$event->setExecutionTime($watch->getDuration()); |
65
|
|
|
|
66
|
|
|
$this->logs->enqueue($event); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function hasLoggedEvents(): bool |
73
|
|
|
{ |
74
|
|
|
return !$this->logs->isEmpty(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return LogEvent |
79
|
|
|
*/ |
80
|
|
|
public function getLoggedEvent(): LogEvent |
81
|
|
|
{ |
82
|
|
|
if (!$this->hasLoggedEvents()) { |
83
|
|
|
throw new LogicException('No more events logged!'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->logs->dequeue(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|