|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bardex\Elastic\Listener; |
|
4
|
|
|
|
|
5
|
|
|
use Bardex\Elastic\IListener; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
|
|
8
|
|
|
class Logger implements IListener |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var LoggerInterface $logger |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $logger; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var bool логировать все запросы |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $logAllQueries=true; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var bool логировать запросы с ошибками |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $logErrorQueries=true; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var bool логировать медленные запросы |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $logSlowQueries=true; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int лимит времени выполнения запроса после которого он считается медленным (мс) |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $slowQueryLimitMs = 1000; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Logger constructor. |
|
37
|
|
|
* @param LoggerInterface $logger |
|
38
|
|
|
* @param bool $logAllQueries |
|
39
|
|
|
* @param bool $logErrorQueries |
|
40
|
|
|
* @param bool $logSlowQueries |
|
41
|
|
|
* @param int $slowQueryLimitMs |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
LoggerInterface $logger, |
|
45
|
|
|
$logAllQueries=true, |
|
46
|
|
|
$logErrorQueries=true, |
|
47
|
|
|
$logSlowQueries=true, |
|
48
|
|
|
$slowQueryLimitMs=1000) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->logger = $logger; |
|
51
|
|
|
$this->logAllQueries = $logAllQueries; |
|
52
|
|
|
$this->logErrorQueries = $logErrorQueries; |
|
53
|
|
|
$this->logSlowQueries = $logSlowQueries; |
|
54
|
|
|
$this->slowQueryLimitMs = $slowQueryLimitMs; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param LoggerInterface $logger |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setLogger(LoggerInterface $logger) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->logger = $logger; |
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param bool $logAllQueries |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setLogAllQueries($logAllQueries) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->logAllQueries = $logAllQueries; |
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param bool $logErrorQueries |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setLogErrorQueries($logErrorQueries) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->logErrorQueries = $logErrorQueries; |
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param bool $logSlowQueries |
|
86
|
|
|
*/ |
|
87
|
|
|
public function setLogSlowQueries($logSlowQueries) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->logSlowQueries = $logSlowQueries; |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param int $slowQueryLimitMs |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setSlowQueryLimitMs($slowQueryLimitMs) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->slowQueryLimitMs = $slowQueryLimitMs; |
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
public function onSuccess(array $query, array $response, $time) |
|
104
|
|
|
{ |
|
105
|
|
|
if ($this->logAllQueries || ($this->logSlowQueries && $time > $this->slowQueryLimitMs)) { |
|
106
|
|
|
$index = $this->getIndexName($query); |
|
107
|
|
|
$context = [ |
|
108
|
|
|
'query' => json_encode($query), |
|
109
|
|
|
'time' => $time, |
|
110
|
|
|
'time_range' => $this->getTimeRange($time), |
|
111
|
|
|
'index' => $index, |
|
112
|
|
|
'found_rows' => $response['hits']['total'], |
|
113
|
|
|
'fetched_rows' => count($response['hits']['hits']) |
|
114
|
|
|
]; |
|
115
|
|
|
|
|
116
|
|
|
if ($this->logAllQueries) { |
|
117
|
|
|
$this->logger->debug("Elastic query (index: $index, time: $time ms)", $context); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if ($this->logSlowQueries && $time > $this->slowQueryLimitMs) { |
|
121
|
|
|
$this->logger->warning("Slow elastic query (index: $index, time: $time ms)", $context); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function onError(array $query, \Exception $e) |
|
127
|
|
|
{ |
|
128
|
|
|
if ($this->logErrorQueries) { |
|
129
|
|
|
$index = $this->getIndexName($query); |
|
130
|
|
|
$context = [ |
|
131
|
|
|
'query' => json_encode($query), |
|
132
|
|
|
'index' => $index, |
|
133
|
|
|
'error' => $e->getMessage(), |
|
134
|
|
|
]; |
|
135
|
|
|
$this->logger->error("Error elastic query (index: $index)", $context); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
protected function getIndexName(array $query) |
|
140
|
|
|
{ |
|
141
|
|
|
$index = isset($query['index']) ? $query['index'] : '(undefined index)'; |
|
142
|
|
|
$type = isset($query['type']) ? $query['type'] : '(undefined type)'; |
|
143
|
|
|
return "$index/$type"; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
protected function getTimeRange($time) |
|
147
|
|
|
{ |
|
148
|
|
|
if ($time <= 10) return '0-10 ms'; |
|
149
|
|
|
if ($time <= 30) return '10-30 ms'; |
|
150
|
|
|
if ($time <= 50) return '30-50 ms'; |
|
151
|
|
|
if ($time <= 100) return '50-100 ms'; |
|
152
|
|
|
if ($time <= 500) return '100-500 ms'; |
|
153
|
|
|
if ($time <= 1000) return '500-1000 ms'; |
|
154
|
|
|
if ($time > 1000) return '> 1000 ms'; |
|
155
|
|
|
} |
|
156
|
|
|
} |