1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhalconExt\Db; |
4
|
|
|
|
5
|
|
|
use Phalcon\Db\Adapter; |
|
|
|
|
6
|
|
|
use Phalcon\Events\Event; |
|
|
|
|
7
|
|
|
use PhalconExt\Logger\LogsToFile; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* SQL logger implemented as db event listener. |
11
|
|
|
* |
12
|
|
|
* @author Jitendra Adhikari <[email protected]> |
13
|
|
|
* @license MIT |
14
|
|
|
* |
15
|
|
|
* @link https://github.com/adhocore/phalcon-ext |
16
|
|
|
*/ |
17
|
|
|
class Logger |
18
|
|
|
{ |
19
|
|
|
use LogsToFile; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
protected $config = []; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $logFormat = "%message%;\n"; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $fileExtension = '.sql'; |
29
|
|
|
|
30
|
|
|
public function __construct(array $config) |
31
|
|
|
{ |
32
|
|
|
$this->config = $config + ['skipFirst' => 0, 'addHeader' => false, 'backtraceLevel' => 0]; |
33
|
|
|
|
34
|
|
|
if (!empty($this->config['enabled']) && !empty($this->config['logPath'])) { |
35
|
|
|
$this->activate($this->config['logPath']); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Run before firing query. |
41
|
|
|
* |
42
|
|
|
* @param Event $event |
43
|
|
|
* @param Adapter $connection |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function beforeQuery(Event $event, Adapter $connection) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
if (!$this->activated) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
static $index = -1; |
54
|
|
|
|
55
|
|
|
if (++$index < $this->config['skipFirst']) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->log($this->getHeader($index) . $this->getBacktrace() . $this->getParsedSql($connection)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get log header like request uri and datetime. |
64
|
|
|
* |
65
|
|
|
* @param int $index The current position count of query. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
protected function getHeader(int $index): string |
70
|
|
|
{ |
71
|
|
|
if ($index !== $this->config['skipFirst'] || !$this->config['addHeader']) { |
72
|
|
|
return ''; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$header = '-- ' . ($_SERVER['REQUEST_URI'] ?? '') . \date(' [Y-m-d H:i:s]') . "\n"; |
76
|
|
|
|
77
|
|
|
return $header |
78
|
|
|
. \str_pad('-- -', \strlen($header), '-', STR_PAD_RIGHT) |
79
|
|
|
. "\n"; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the backtrace of paths leading to logged query. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function getBacktrace(): string |
88
|
|
|
{ |
89
|
|
|
if ($this->config['backtraceLevel'] < 1) { |
90
|
|
|
return ''; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$trace = ''; |
94
|
|
|
foreach (\array_slice(\debug_backtrace(), 2, $this->config['backtraceLevel']) as $tr) { |
95
|
|
|
$tr += ['function' => 'n/a', 'line' => 0, 'file' => 'n/a']; |
96
|
|
|
|
97
|
|
|
$trace .= " -- {$tr['file']}:{$tr['line']}#{$tr['function']}()\n"; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $trace; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the properly interpolated sql. |
105
|
|
|
* |
106
|
|
|
* @param Adapter $connection |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function getParsedSql($connection): string |
111
|
|
|
{ |
112
|
|
|
$parts = $connection->convertBoundParams( |
113
|
|
|
$connection->getSqlStatement(), |
114
|
|
|
$connection->getSQLVariables() ?: [] |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$binds = $parts['params'] ?: $connection->getSQLVariables(); |
118
|
|
|
|
119
|
|
|
return \vsprintf(\str_replace('?', "'%s'", $parts['sql']), $binds); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Run after firing query. |
124
|
|
|
* |
125
|
|
|
* @param Event $event |
126
|
|
|
* @param Adapter $connection |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function afterQuery(Event $event, $connection) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
// Do nothing. |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths