|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Phalcon Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Phalcon Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* Implementation of this file has been influenced by AtlasPHP |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/atlasphp/Atlas.Pdo |
|
14
|
|
|
* @license https://github.com/atlasphp/Atlas.Pdo/blob/1.x/LICENSE.md |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
declare(strict_types=1); |
|
18
|
|
|
|
|
19
|
|
|
namespace Phalcon\DataMapper\Pdo\Profiler; |
|
20
|
|
|
|
|
21
|
|
|
use Phalcon\DataMapper\Pdo\Exception\Exception; |
|
22
|
|
|
use Phalcon\Helper\Json; |
|
23
|
|
|
use Psr\Log\LoggerInterface; |
|
24
|
|
|
use Psr\Log\LogLevel; |
|
25
|
|
|
|
|
26
|
|
|
use function microtime; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Sends query profiles to a logger. |
|
30
|
|
|
* |
|
31
|
|
|
* @property bool $active |
|
32
|
|
|
* @property array $context |
|
33
|
|
|
* @property string $logFormat |
|
34
|
|
|
* @property string $logLevel |
|
35
|
|
|
* @property LoggerInterface $logger |
|
36
|
|
|
*/ |
|
37
|
|
|
class Profiler implements ProfilerInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var bool |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $active = false; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $context = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $logFormat = "{method} ({duration}s): {statement} {backtrace}"; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $logLevel = LogLevel::DEBUG; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var LoggerInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $logger; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Constructor. |
|
66
|
|
|
* |
|
67
|
|
|
* @param LoggerInterface $logger |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct(LoggerInterface $logger = null) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($logger === null) { |
|
72
|
|
|
$logger = new MemoryLogger(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->logger = $logger; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Finishes and logs a profile entry. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $statement |
|
82
|
|
|
* @param array $values |
|
83
|
|
|
*/ |
|
84
|
|
|
public function finish(string $statement = null, array $values = []): void |
|
85
|
|
|
{ |
|
86
|
|
|
if ($this->active) { |
|
87
|
|
|
$finish = microtime(true); |
|
88
|
|
|
$ex = new Exception(); |
|
89
|
|
|
|
|
90
|
|
|
$this->context['backtrace'] = $ex->getTraceAsString(); |
|
91
|
|
|
$this->context['duration'] = $finish - $this->context['start']; |
|
92
|
|
|
$this->context['finish'] = $finish; |
|
93
|
|
|
$this->context['statement'] = $statement; |
|
94
|
|
|
$this->context['values'] = empty($values) ? '' : Json::encode($values); |
|
95
|
|
|
|
|
96
|
|
|
$this->logger->log($this->logLevel, $this->logFormat, $this->context); |
|
97
|
|
|
|
|
98
|
|
|
$this->context = []; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Returns the log message format string, with placeholders. |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getLogFormat(): string |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->logFormat; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns the underlying logger instance. |
|
114
|
|
|
* |
|
115
|
|
|
* @return LoggerInterface |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getLogger(): LoggerInterface |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->logger; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns the level at which to log profile messages. |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getLogLevel(): string |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->logLevel; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns true if logging is active. |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function isActive(): bool |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->active; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Enable or disable profiler logging. |
|
144
|
|
|
* |
|
145
|
|
|
* @param bool $active |
|
146
|
|
|
* |
|
147
|
|
|
* @return ProfilerInterface |
|
148
|
|
|
*/ |
|
149
|
|
|
public function setActive($active): ProfilerInterface |
|
150
|
|
|
{ |
|
151
|
|
|
$this->active = (bool) $active; |
|
152
|
|
|
|
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Sets the log message format string, with placeholders. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $logFormat |
|
160
|
|
|
* |
|
161
|
|
|
* @return ProfilerInterface |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setLogFormat($logFormat): ProfilerInterface |
|
164
|
|
|
{ |
|
165
|
|
|
$this->logFormat = $logFormat; |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Level at which to log profile messages. |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $logLevel |
|
174
|
|
|
* |
|
175
|
|
|
* @return ProfilerInterface |
|
176
|
|
|
*/ |
|
177
|
|
|
public function setLogLevel($logLevel): ProfilerInterface |
|
178
|
|
|
{ |
|
179
|
|
|
$this->logLevel = $logLevel; |
|
180
|
|
|
|
|
181
|
|
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Starts a profile entry. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $method |
|
188
|
|
|
*/ |
|
189
|
|
|
public function start(string $method): void |
|
190
|
|
|
{ |
|
191
|
|
|
if ($this->active) { |
|
192
|
|
|
$this->context = [ |
|
193
|
|
|
"method" => $method, |
|
194
|
|
|
"start" => microtime(true), |
|
195
|
|
|
]; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|