1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GBProd\ElasticaBundle\Logger; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Logger for the Elastica. |
9
|
|
|
* |
10
|
|
|
* The {@link logQuery()} method is configured as the logger callable in the |
11
|
|
|
* service container. |
12
|
|
|
* |
13
|
|
|
* @author Gordon Franke <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class ElasticaLogger implements LoggerInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var LoggerInterface |
19
|
|
|
*/ |
20
|
|
|
protected $logger; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $queries = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var boolean |
29
|
|
|
*/ |
30
|
|
|
protected $debug; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
* |
35
|
|
|
* @param LoggerInterface|null $logger The Symfony logger |
36
|
|
|
* @param boolean $debug |
37
|
|
|
*/ |
38
|
24 |
|
public function __construct(LoggerInterface $logger = null, $debug = false) |
39
|
|
|
{ |
40
|
24 |
|
$this->logger = $logger; |
41
|
24 |
|
$this->debug = $debug; |
42
|
24 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Logs a query. |
46
|
|
|
* |
47
|
|
|
* @param string $path Path to call |
48
|
|
|
* @param string $method Rest method to use (GET, POST, DELETE, PUT) |
49
|
|
|
* @param array $data Arguments |
50
|
|
|
* @param float $time Execution time |
51
|
|
|
* @param array $connection Host, port, transport, and headers of the query |
52
|
|
|
* @param array $query Arguments |
53
|
|
|
*/ |
54
|
4 |
|
public function logQuery($path, $method, $data, $time, $connection = array(), $query = array(), $engineTime = 0, $itemCount = 0) |
|
|
|
|
55
|
|
|
{ |
56
|
4 |
|
if ($this->debug) { |
57
|
2 |
|
$e = new \Exception(); |
|
|
|
|
58
|
|
|
|
59
|
2 |
|
$this->queries[] = array( |
60
|
2 |
|
'path' => $path, |
61
|
2 |
|
'method' => $method, |
62
|
2 |
|
'data' => $data, |
63
|
2 |
|
'executionMS' => $time, |
64
|
2 |
|
'engineMS' => $engineTime, |
65
|
2 |
|
'connection' => $connection, |
66
|
2 |
|
'queryString' => $query, |
67
|
2 |
|
'itemCount' => $itemCount, |
68
|
2 |
|
'backtrace' => $e->getTraceAsString() |
69
|
2 |
|
); |
70
|
2 |
|
} |
71
|
|
|
|
72
|
4 |
|
if (null !== $this->logger) { |
73
|
1 |
|
$message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000); |
|
|
|
|
74
|
1 |
|
$this->logger->info($message, (array) $data); |
75
|
1 |
|
} |
76
|
4 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the number of queries that have been logged. |
80
|
|
|
* |
81
|
|
|
* @return integer The number of queries logged |
82
|
|
|
*/ |
83
|
3 |
|
public function getNbQueries() |
84
|
|
|
{ |
85
|
3 |
|
return count($this->queries); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns a human-readable array of queries logged. |
90
|
|
|
* |
91
|
|
|
* @return array An array of queries |
92
|
|
|
*/ |
93
|
1 |
|
public function getQueries() |
94
|
|
|
{ |
95
|
1 |
|
return $this->queries; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
2 |
|
public function emergency($message, array $context = array()) |
102
|
|
|
{ |
103
|
2 |
|
if (!$this->logger) { |
104
|
1 |
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
return $this->logger->emergency($message, $context); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
2 |
|
public function alert($message, array $context = array()) |
114
|
|
|
{ |
115
|
2 |
|
if (!$this->logger) { |
116
|
1 |
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return $this->logger->alert($message, $context); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
2 |
|
public function critical($message, array $context = array()) |
126
|
|
|
{ |
127
|
2 |
|
if (!$this->logger) { |
128
|
1 |
|
return; |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
return $this->logger->critical($message, $context); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
2 |
|
public function error($message, array $context = array()) |
138
|
|
|
{ |
139
|
2 |
|
if (!$this->logger) { |
140
|
1 |
|
return; |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
return $this->logger->error($message, $context); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
2 |
|
public function warning($message, array $context = array()) |
150
|
|
|
{ |
151
|
2 |
|
if (!$this->logger) { |
152
|
1 |
|
return; |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
return $this->logger->warning($message, $context); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
2 |
|
public function notice($message, array $context = array()) |
162
|
|
|
{ |
163
|
2 |
|
if (!$this->logger) { |
164
|
1 |
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
return $this->logger->notice($message, $context); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
2 |
|
public function info($message, array $context = array()) |
174
|
|
|
{ |
175
|
2 |
|
if (!$this->logger) { |
176
|
1 |
|
return; |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
return $this->logger->info($message, $context); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
2 |
|
public function debug($message, array $context = array()) |
186
|
|
|
{ |
187
|
2 |
|
if (!$this->logger) { |
188
|
1 |
|
return; |
189
|
|
|
} |
190
|
|
|
|
191
|
1 |
|
return $this->logger->debug($message, $context); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
1 |
|
public function log($level, $message, array $context = array()) |
198
|
|
|
{ |
199
|
1 |
|
if (!$this->logger) { |
200
|
|
|
return; |
201
|
|
|
} |
202
|
|
|
|
203
|
1 |
|
return $this->logger->log($level, $message, $context); |
204
|
|
|
} |
205
|
|
|
} |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.