-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.php
169 lines (141 loc) · 4.73 KB
/
Logger.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Nelmio\SolariumBundle;
use Solarium\Core\Client\Request as SolariumRequest;
use Solarium\Core\Client\Response as SolariumResponse;
use Solarium\Core\Client\Endpoint as SolariumEndpoint;
use Solarium\Core\Plugin\Plugin as SolariumPlugin;
use Solarium\Core\Event\Events as SolariumEvents;
use Solarium\Core\Event\PreExecuteRequest as SolariumPreExecuteRequestEvent;
use Solarium\Core\Event\PostExecuteRequest as SolariumPostExecuteRequestEvent;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Symfony\Component\HttpFoundation\Response as HttpResponse;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
class Logger extends SolariumPlugin implements DataCollectorInterface, \Serializable
{
private $data = array();
private $queries = array();
private $currentRequest;
private $currentStartTime;
private $currentEndpoint;
private $logger;
private $stopwatch;
/**
* Plugin init function
*
* Register event listeners
*/
protected function initPluginType()
{
$dispatcher = $this->client->getEventDispatcher();
$dispatcher->addListener(SolariumEvents::PRE_EXECUTE_REQUEST, array($this, 'preExecuteRequest'), 1000);
$dispatcher->addListener(SolariumEvents::POST_EXECUTE_REQUEST, array($this, 'postExecuteRequest'), -1000);
}
/**
* Set the Logger
*
* @param LoggerInterface $logger
*/
public function setLogger($logger)
{
$this->logger = $logger;
}
/**
* Set the Stopwatch
*
* @param Stopwatch $stopwatch
*/
public function setStopwatch($stopwatch)
{
$this->stopwatch = $stopwatch;
}
public function log(SolariumRequest $request, $response, SolariumEndpoint $endpoint, $duration)
{
$this->queries[] = array(
'request' => $request,
'response' => $response,
'duration' => $duration,
'base_uri' => $endpoint->getBaseUri(),
);
}
public function collect(HttpRequest $request, HttpResponse $response, \Exception $exception = null)
{
if (isset($this->currentRequest)) {
$this->failCurrentRequest();
}
$time = 0;
foreach ($this->queries as $queryStruct) {
$time += $queryStruct['duration'];
}
$this->data = array(
'queries' => $this->queries,
'total_time' => $time,
);
}
public function getName()
{
return 'solr';
}
public function getQueries()
{
return array_key_exists('queries', $this->data) ? $this->data['queries'] : array();
}
public function getQueryCount()
{
return count($this->getQueries());
}
public function getTotalTime()
{
return array_key_exists('total_time', $this->data) ? $this->data['total_time'] : 0;
}
public function preExecuteRequest(SolariumPreExecuteRequestEvent $event)
{
if (isset($this->currentRequest)) {
$this->failCurrentRequest();
}
if (null !== $this->stopwatch) {
$this->stopwatch->start('solr', 'solr');
}
$this->currentRequest = $event->getRequest();
$this->currentEndpoint = $event->getEndpoint();
if (null !== $this->logger) {
$this->logger->debug($event->getEndpoint()->getBaseUri() . $this->currentRequest->getUri());
}
$this->currentStartTime = microtime(true);
}
public function postExecuteRequest(SolariumPostExecuteRequestEvent $event)
{
$endTime = microtime(true) - $this->currentStartTime;
if (!isset($this->currentRequest)) {
throw new \RuntimeException('Request not set');
}
if ($this->currentRequest !== $event->getRequest()) {
throw new \RuntimeException('Requests differ');
}
if (null !== $this->stopwatch) {
$this->stopwatch->stop('solr');
}
$this->log($event->getRequest(), $event->getResponse(), $event->getEndpoint(), $endTime);
$this->currentRequest = null;
$this->currentStartTime = null;
$this->currentEndpoint = null;
}
public function failCurrentRequest()
{
$endTime = microtime(true) - $this->currentStartTime;
if (null !== $this->stopwatch) {
$this->stopwatch->stop('solr');
}
$this->log($this->currentRequest, null, $this->currentEndpoint, $endTime);
$this->currentRequest = null;
$this->currentStartTime = null;
$this->currentEndpoint = null;
}
public function serialize()
{
return serialize($this->data);
}
public function unserialize($data)
{
$this->data = unserialize($data);
}
}